What is Changed?
“Changed” refers to a method, or built in function that is a part of Roblox Studio. It can be thought of as a listener. Its job is to listen for changes to the value that it has been attached to, then do something when that value changes.
What is a Value?
The code below when placed in a Script in the ServerScriptService will do the following.
Create a leaderboard for each player
Create a Gold value for each player and place it on the leaderboard.
Increase the Gold value every 2 seconds.
The important thing to take note of is the plrGold.Value. This is an example of something that then Changed method can be used to listen to.
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
local leaderstats = Instance.new("Folder",player)
leaderstats.Name = "leaderstats"
local plrGold = Instance.new("NumberValue",leaderstats)
plrGold.Name = "Gold"
plrGold.Value = 0
task.spawn(function()
while task.wait(2) do
plrGold.Value += 10
end
end)
end)
end)
How To Use Changed
To see the changes to the plrGold value above we can create a Local Script and place it in the StarterCharacterScripts folder as an example.
In the local script add the following code.
local Players = game:GetService("Players")
local player = Players.LocalPlayer
player.leaderstats.Gold.Changed:Connect(function()
print("Gold Changed")
end)
As you can see we are accessing the players Gold Value and attaching the Changed method to it. We then connect a function that will show the print message in the output window.
You can use the Changed method on any object that has a VALUE attached to it. Just remember that you use Changed on the name of the value not the value itself.
Comments