Value Objects
Overview
When using value objects such as IntValue or StringValue, the client can directly access server-side values without needing communication processes like RemoteEvent. This optimizes and reduces the code’s complexity, and helps maintain a more straightforward network communication structure.
How to Use
Supported Data Types
IntValue
O
Integer values only
NumberValue
O
Includes integers and float values
StringValue
O
BoolValue
O
ObjectValue
X
CFrameValue
X
Vector3Value
X
Color3Value
X
How to Use
When a value object’s value is changed by the server, the client can read the value directly without using a separate RemoteEvent. This streamlines the communication structure, making the code easier to maintain and debug.
Additionally, instead of managing key game data, such as monster damage, HP, or player scores, through script variables, you can structure and manage this data at the object level. This facilitates the debugging process and allows you to visually monitor through the Level Browser, which can significantly improve development efficiency.
Since designers and non-developers can modify values directly and connect objects through the Studio’s property panel, this structure is effective in collaborative environments with non-programmers.
By connecting the Changed event to a value object, you can trigger only the necessary processes when the value changes. This greatly improves the visibility of the overall data flow, and makes it easier to manage and debug.
When the Changed event for the same value object is connected in the client, the corresponding process is triggered whenever the value changes.
Usage Examples
When the player’s HP changes on the server, the client detects the change in the NumberValue and refreshes the HPBar UI accordingly.
When the server processes a skill activation, the client detects the change in the BoolValue and disables the skill button accordingly.
When the server changes the activation status of a certain object, the client detects the change in the BoolValue and displays the corresponding UI icon.
When the server changes the player’s status (e.g., stunned, fainted, etc.), the client detects the change in the BoolValue or StringValue and applies the effect on the screen.
Important Notes
To prevent issues with loading time, it is recommended to always use WaitForChild() when referencing a value object.
A value object only synchronizes with the client when the server changes its value. However, if the client changes the value, it will not be synced with other clients or the server.
This method is not suitable for handling complex structures or large-scale data, but it works well for simple status values or individual pieces of information.
Excessive use of value objects can clutter the Level Browser, and make it difficult to analyze the structure. Proper folder organization and naming conventions are required.
If the values change frequently, excessive use of the Changed event may impact performance. It is important to disconnect unnecessary connections using Disconnect().
Sensitive values that must be secure should only be stored in server-only areas, such as ServerScriptService, to prevent them from being exposed to the client.
Last updated