Infinite Print For Some Reason

I am trying to make a system that when a bool value is updated from true or false it unlocks an item for the player. The system should check if the player has the bool value at true when they enter and if they buy it while playing.

The system seemed to work fine but it beeped printing. No errors just keep printing “Changed”.

local plr = game.Players.LocalPlayer
local RS = game:GetService("RunService")
local leaderstats = plr:WaitForChild("Values")
local btn = script.Parent


if leaderstats:WaitForChild("CandyUnlock").Value == true then
	btn.Text = "Unlocked"
	print("Default Changed")
end


local c
c = RS.RenderStepped:Connect(function()
	wait()
	if leaderstats:WaitForChild("CandyUnlock").Value == true then
		btn.Text = "Unlocked"
		print("Changed")
	end
end)

As you can see on that bottom section. If there is a better way to check if a value has been updated that would be a valid solution as well.

Thank You,
Madd

Simple:

leaderstats:WaitForChild("CandyUnlock").Changed:Connect(function(newVal)
    if newVal == true then
        btn.Text = "Unlocked"
        print("Changed")
    end
end)
1 Like
leaderstats:WaitForChild("CandyUnlock"):GetPropertyChangedSignal("Value"):Connect(function()
	print(leaderstats:WaitForChild("CandyUnlock").Value) --true or false, no need for the render stepped, replace the code you provided with this
end)
1 Like
local plr = game.Players.LocalPlayer
local RS = game:GetService("RunService")
local leaderstats = plr:WaitForChild("Values")
local btn = script.Parent

local function CandyChanged(Value)
	print("CandyUnlock",Value)
	btn.Text = Value and "Unlocked" or btn.Text
end

local CandyUnlock = leaderstats:WaitForChild("CandyUnlock")
CandyUnlock.Changed:Connect(CandyChanged)
1 Like

@NicoleSydor And @msix29 there is no point in using GetPropertyChangedSignal over .Changed, it just takes more time.

1 Like

In this case there isn’t, because Changed only fires when the Value of a ValueBase changes.

But for other use cases, instead of listening for when every property changes, you can just listen for the specific one that’s needed

1 Like

I am aware, one of the main reasons I made that reply because for some reason A LOT of people still think it fires for everything, even in circumstances like this.

2 Likes

This is exactly why you shouldn’t connect lambas (anonymous) functions to signals/events.

local function OnCandyChanged(Value)
	btn.Text = Value and "Unlocked" or btn.Text
end

CandyUnlock.Changed:Connect(OnCandyChanged)
OnCandyChanged(CandyUnlock.Value) --We can just call the function defined earlier.

I never saw you wrote about changed…

1 Like