Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Find out how much a value has changed? [ :Changed() ]

Asked by
Oficcer_F 207 Moderation Voter
6 years ago

How can I find out how much a value has changed? Here is my script:

01game.Players.PlayerAdded:Connect(function(player)
02 
03    local leaderstats = Instance.new("Folder")
04    leaderstats.Parent = player
05    leaderstats.Name = "leaderstats"
06 
07    local points = Instance.new("IntValue")
08    points.Parent = leaderstats
09    points.Name = "Points"
10 
11    oldValue = player.leaderstats.Points.Value
12 
13    player.leaderstats.Points.Changed:Connect(function(newValue)
14 
15    local pointsChanged = newValue - oldValue
16    print(pointsChanged)
17 
18    end)   
19 
20end)

This onlly gives me the value.... Not how much it has changed from the old value...

1 answer

Log in to vote
1
Answered by 6 years ago

You never update the oldValue which is probably 0 so it will not change the new value.

Secondly you have cariables for the IntValue so use them.

Lastly oldValue should also be a local variable.

Example

01-- you should use get service where possible
02-- incase the name if the service is changed
03game:GetService("Players").PlayerAdded:Connect(function(plr)
04 
05     local leaderstats = Instance.new("Folder")
06     leaderstats.Name = "leaderstats"
07 
08     local points = Instance.new("IntValue")
09     points.Name = "Points"
10     points.Parent = leaderstats
11 
12     local oldValue = points.Value -- use the variable
13     points.Changed:Connect(function(newVal)
14         print(newValue - oldValue)
15         oldValue = newValue -- update the old value variable
16     end)
17 
18    -- parent to the game last
19    leaderstats.Parent = plr
20end)

I hope this helps.

0
Should I use this to prevent people from change their points? Besides having FE. Oficcer_F 207 — 6y
0
FE is all you would need to prevent players from changing their points. User#5423 17 — 6y
0
Ok thanks Oficcer_F 207 — 6y
Ad

Answer this question