How can I find out how much a value has changed? Here is my script:
01 | game.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 |
20 | end ) |
This onlly gives me the value.... Not how much it has changed from the old value...
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 |
03 | game: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 |
20 | end ) |
I hope this helps.