[~Solved] Quick! I need help with instance.changed and dbounce!

Hello so I have this script that shows the following output:


here’s the script:

dbounce = false
PlayerCoins.Changed:Connect(function(NewVal)
	if NewVal == OldVal then return end
	if dbounce == false then
		dbounce = true
		print("New Value = ".. NewVal)
		print("Old Value = ".. OldVal)
		local Difference = NewVal - OldVal
		print("Difference = ".. Difference)
		-- calculations
		EventCoins = EventCoins + Difference
		Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
		print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
		-- end
		OldVal = NewVal -- for next execution, this will be used as the new OldVal.
		wait(0.5)
		dbounce = false
	end
end)


why does it not work as intended? I’m hosting a big event so pelase help with this as fast as possible.

It’s located as a script in CharacterScripts

Is that the full script. I assume not hence the error and you don’t show any Player/PlayerCoins variable. Please show the full thing.

Here’s the full script then :slight_smile:

warn("~~Events Loaded~~")
local Players = game:GetService("Players")
local PlayerCharacter = script.Parent
local Player = game:GetService("Players"):FindFirstChild(PlayerCharacter.Name) or Players:GetPlayerFromCharacter(PlayerCharacter.Name)
local PlayerCoins = Player:WaitForChild("leaderstats"):WaitForChild("Coins") -- the players coins
local EventCoinsDS = game:GetService("DataStoreService"):GetOrderedDataStore(game:GetService("ReplicatedStorage"):WaitForChild("EventDataStoreName")) -- gets data from data store
local success,Data = pcall(function()
	return EventCoins:GetAsync(Player.UserId)
end)
local Eventcoins = Data
if EventCoins == nil then
	EventCoins = 0
end

---

wait(5)
warn("~~Events Started~~")
OldVal = PlayerCoins.Value -- Player's current coins

dbounce = false
PlayerCoins.Changed:Connect(function(NewVal)
	if NewVal == OldVal then return end
	if dbounce == false then
		dbounce = true
		print("New Value = ".. NewVal)
		print("Old Value = ".. OldVal)
		local Difference = NewVal - OldVal
		print("Difference = ".. Difference)
		-- calculations
		EventCoins = EventCoins + Difference
		Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
		print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
		-- end
		OldVal = NewVal -- for next execution, this will be used as the new OldVal.
		wait(0.5)
		dbounce = false
	end
end)


I cant find anything. Please help somehow

Change that to:
local Player = Players:GetPlayerFromCharacter(PlayerCharacter)

Try removing that as it is unnecessary. Otherwise, I don’t really know why this error is occuring

The .Changed event does not return the new value, it returns the PROPERTY that is changed.

I would look into using :GetPropertyChangedSignal(“Value”). This fires when the “Value” property is changed. Unfortunately, this doesn’t return the “new value”, so you will have to change that yourself.

dbounce = false
PlayerCoins:GetPropertyChangedSignal("Value"):Connect(function()
	local NewVal = PlayerCoins.Value
	if NewVal == OldVal then return end
	if dbounce == false then
		dbounce = true
		print("New Value = ".. NewVal)
		print("Old Value = ".. OldVal)
		local Difference = NewVal - OldVal
		print("Difference = ".. Difference)
		-- calculations
		EventCoins = EventCoins + Difference
		Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
		print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
		-- end
		OldVal = NewVal -- for next execution, this will be used as the new OldVal.
		wait(0.5)
		dbounce = false
	end
end)

Sources:
https://developer.roblox.com/en-us/api-reference/function/Instance/GetPropertyChangedSignal
https://developer.roblox.com/en-us/api-reference/event/Instance/Changed

There’s another post like this which is already solved, see if it helps in any way: Maximum event re-entrancy depth exceeded for NumberValue.Changed

.Changed event in THIS CASE (or any other case that has a Value instance such as Int/String/Bool/Number ect.) DOES return the new value, in any other case you would be right.

1 Like

Maybe I’m missing something :thinking:

Would you mind explaining why this is returning the new value? As far as I understand, PlayerCoins is an IntValue.

Edit: Nevermind!! Looking at the docs close up, I was missing something and you’re completely right!

This error is caused because you are changing the item in a .Changed event which will fire itself over and over etc, i did NOT read the script, i just said the most common reason

I searched everywhere the problem is that the event is being fired too fast, you need to add some debounce and see what it works.

I did, read the script again. :slight_smile:

So which one should I use for this scenario?

True. But that’s where I need some sort of solution. It seems like the debounce works incorrectly.

1 Like

I looked at it earlier this evening but all they do is add debounce. This just does not seem to work for me…

i don’t know if this information is useful by the way, but might it have anything to do with the fact that there are several of these scripts? Like every startercharacterscript gets clones into each player? I know they shouldn’t overlap as each script has control over one players’ value, but still. :confused:

I have one alternative to this whole issue and that is to delete the entire code and find all scripts that changes the coin value, then change the events value as well on the line below that.

I think this will fix it…

dbounce = false
PlayerCoins.Changed:Connect(function(NewVal)
	if NewVal == OldVal then return end
	if dbounce == true then return end
	dbounce = true
	delay(.5 , function()
		dbounce = false
	end)
	print("New Value = ".. NewVal)
	print("Old Value = ".. OldVal)
	local Difference = NewVal - OldVal
	print("Difference = ".. Difference)
	-- calculations
	EventCoins = EventCoins + Difference		Player.Event.Value = EventCoins -- change value so player can rejoin and see the difference :)
	print("Changed ".. Player.Name .."'s Event value from ".. OldVal .." to ".. NewVal .." with int difference ".. Difference ..".")
	-- end
	OldVal = NewVal -- for next execution, this will be used as the new OldVal.
end)