.Changed loop workaround?

I’m trying to multiply a value everytime it has been changed but it creates a loop for .Changed which causes it to run more than intended.

local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
Value.Changed:Connect(function(NewVal)
	game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2
end)

Note that my original plan was to see how much the value has exactly been changed then multiply it by 2.

1 Like

Can you try to elaborate more on the issue I can’t quite understand.

1 Like

Okay so basically, when deeds value changed, i want it to be multipled by two, but since it also changes, .Changed event will call again thus multiplying the number even further. For example, changing deeds from 500 to intended 1000 will actually change it to a higher number due to the loop.

1 Like

So when are you wanting the loop to end? What are you trying to achieve?

1 Like

The loop in question is .Changed running more than intended due to this

Value.Changed:Connect(function(NewVal)
	game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2 -- Main culprit, causing .Changed to run again because same instance.
end)

But I couldn’t find any other solutions.

1 Like

Yes I understand that is making it run again. But I need to know when you are wanting the loop to stop or how you are wanting the work around to work.

1 Like

I just want it to only run once. A Debounce wouldn’t work since the value is changed every 0.4 seconds excluding the multiplication.

1 Like

return the function once it’s run

1 Like

Don’t use .Changed for this, instead do this when the Value is being changed by another script, you are basically telling the item to change while the .Changed Connection is there, because of that it will continue firing as a change happened to the object you are changing, it will keep doing this until it breaks.

This wont do anything, it will still detect the changes, and continue to loop

does return not end a function though? i thought it would change the value and then end, only picking up on another change in the future

(oh yeah i just realized nevermind)

1 Like

Yeah, I figured there is no possible solutions so I just used a function that is primary and heavily used for giving out Deeds.

function PlayerModule:Click(Player, Deed)
	if workspace:FindFirstChild("Crown") then
		Player:FindFirstChild("leaderstats"):FindFirstChild("Deeds").Value += Deed + Core.Values.HasCarpet:FindFirstChild("Bonus").Value * 2
	else
		Player:FindFirstChild("leaderstats"):FindFirstChild("Deeds").Value += Deed + Core.Values.HasCarpet:FindFirstChild("Bonus").Value	
	end
end
1 Like

That’s not how that works, the Connection fires the function given which is returning something, it doesnt end the connection

Try this:

local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
Value:GetPropertyChangedSignal("Value"):Connect(function()
	game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value *= 2
end)

:GetPropertyChangedSignal() will only fire when the given property changes rather than .Changed since it fires when any property of a given instance is changed

If i’m understanding your problem correctly it should solve your problem.

This would still cause the Event to loop as you are changing the value within the Connection that is listening to for changes.

For immediate signal behavior:

local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
local debounce = false
Value.Changed:Connect(function(NewVal)
if debounce then return end
debounce = true
game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2
debounce = false
end)

For deferred signal behavior:

local Value = game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds
local debounce = false
Value.Changed:Connect(function(NewVal)
if debounce then return end
debounce = true
game:GetService("Players"):FindFirstChild(workspace.Moai.Moai.Owner.Value).leaderstats.Deeds.Value = NewVal * 2
task.wait(.1)
debounce = false
end)

Why is this thread 15 posts long at this point? This was easy to solve.

use :GetPropertyChangedSignal()

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