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.
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)
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)
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.
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.