Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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

✓ Accepted answer

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.

3 comments

0

Should I use this to prevent people from change their points? Besides having FE.

0

FE is all you would need to prevent players from changing their points.

0

Ok thanks

This discussion is preserved as it was. New answers, comments, and votes are closed.