How would i detect a number value has increased by how much

So basically when a value changes like maybe a kill value in the leaderstats changes, the script tells me by how much, like your kills was 2 and you got 4 kills from an explosion, it would say +4 kills, but i currently dont know how to do that.

i tried looking through the devforum and i cant seem to find any posts about my problem, sorry if im describing this badly this is my first post as i dont like asking help, if your confused im happy to explain more.

1 Like

Keep a record of what the number was, and when it changes just compare the two.

1 Like

Have 2 separate variables for each player.

One that is updated in real time. For example, it updates right after a kill is confirmed.

The other variable will update only after a GetPropertyChangedSignal (search that up!) shows an update in the player’s kills.

Use this update to trigger a block of code that compares the real time variable to the second one…

…and there you go!

1 Like

All isntances have a Instance:GetPropertyChangedSignal(PropertyName) function which returns the event fired when that particular property is fired. For example, to detect a value change you would do:

local value = game.Workspace.NumberValue

value:GetPropertyChangedSignal("Value"):Connect(function()
    print("New value: " .. value.Value)
end)

wait(5)
value.Value = 4

After 5 seconds, you should see this in the console: image

1 Like

the problem is that it prints the new value, for example

game:GetService("Players").LocalPlayer:WaitForChild("leaderstats").Kills.Changed:Connect(funtion(newValue)
print(newValue)
end)

it would print the new value rather than printing how much it added i tried adding variables to compare it but i cant seem to get a grasp on how to actually compare it and do the math

Yes but how would i be able to compare the old value to the new value? i know there is < and > but im looking for how much the new value added to the old value

1 Like

I mean you can indeed store the old value and compare it to the new value. Let me give you a little example of how you can make this:

First you need to make two variables and in this example I’ll name one oldVar and one newVar. At the start both oldVar and newVar should be equal to 0 as the player hasn’t made any kills yet. If the player manages to make a kill it should add 1 to the newVar. Then you can calculate the difference by simply substracting the new value (newVar) from the old value (oldVar)

game:GetService("Players").LocalPlayer:WaitForChild("leaderstats").Kills.Changed:Connect(function(newVar)
    print("Difference = " + newVar - oldVar)
end)

After you have calculated the difference, you should make the oldVar equal to the newVar.

game:GetService("Players").LocalPlayer:WaitForChild("leaderstats").Kills.Changed:Connect(function(newVar)
    print("Difference = " + newVar - oldVar)
    oldVar = newVar
end)

If the player manages to get another kill, the process repeats. I hope this will help! :slight_smile:

2 Likes

i completely forgot about subtraction :man_facepalming: i need to get more sleep haha. Thank you a lot for the help!

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.