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
5 years ago

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

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = player
    leaderstats.Name = "leaderstats"

    local points = Instance.new("IntValue")
    points.Parent = leaderstats
    points.Name = "Points"

    oldValue = player.leaderstats.Points.Value

    player.leaderstats.Points.Changed:Connect(function(newValue)

    local pointsChanged = newValue - oldValue
    print(pointsChanged)

    end)    

end)

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 5 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

-- you should use get service where possible 
-- incase the name if the service is changed
game:GetService("Players").PlayerAdded:Connect(function(plr)

     local leaderstats = Instance.new("Folder")
     leaderstats.Name = "leaderstats"

     local points = Instance.new("IntValue")
     points.Name = "Points"
     points.Parent = leaderstats

     local oldValue = points.Value -- use the variable
     points.Changed:Connect(function(newVal)
         print(newValue - oldValue)
         oldValue = newValue -- update the old value variable
     end)

    -- parent to the game last
    leaderstats.Parent = plr
end)

I hope this helps.

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

Answer this question