How would I add a changed function here?

Hello, I am trying to implement a changed function into this script and I’m not totally sure how I would go about that. I have tried it a few different ways but none of them seem to work.

local cp = 0
local players = game.Players.LocalPlayer

while wait() do
	if cp~= players.leaderstats.Candy.Value then
		local random = math.random(1,1900)
		local xnew = random/1000
		local new = game.ReplicatedStorage.ShowAdd:Clone()
		new.Parent = script.Parent.ScreenGui
		new.Position = UDim2.new(xnew,0,1,0)
		new.Text = "+"..players.leaderstats.Candy.Value - cp.."🍬"
		cp = players.leaderstats.Candy.Value
	end

end

Do you mean something like this?

players.leaderstats.Candy:GetPropertyChangedSignal("Value"):Connect(functon()
	if cp~= players.leaderstats.Candy.Value then
		local random = math.random(1,1900)
		local xnew = random/1000
		local new = game.ReplicatedStorage.ShowAdd:Clone()
		new.Parent = script.Parent.ScreenGui
		new.Position = UDim2.new(xnew,0,1,0)
		new.Text = "+"..players.leaderstats.Candy.Value - cp.."🍬"
		cp = players.leaderstats.Candy.Value
	end
end)

hmm, it only seems to make the gui appear sometimes and not all the time which is why I wanted to add a changed function.

I’m a bit confused on what you mean, the code above fires when the players candy’s amount/value is changed.

I know, I’m a bit confused aswell. It appears most of the time when the points change but sometimes it doesn’t appear.

Maybe it is the if cp~= players.leaderstats.Candy.Value then?

I think it might be that because it goes off when cp isn’t equal to the player’s points and it changes to the players points at the end so the next time it runs it wouldn’t go off.

Oh, so try.

players.leaderstats.Candy:GetPropertyChangedSignal("Value"):Connect(functon()
	local random = math.random(1,1900)
	local xnew = random/1000
	local new = game.ReplicatedStorage.ShowAdd:Clone()
	new.Parent = script.Parent.ScreenGui
	new.Position = UDim2.new(xnew,0,1,0)
	new.Text = "+"..players.leaderstats.Candy.Value - cp.."🍬"
	cp = players.leaderstats.Candy.Value
end)

Try this:

local cp = 0
local players = game.Players.LocalPlayer
local can = players:WaitForChild("leaderstats"):WaitForChild("Candy")

can.Changed:Connect(function()
	local random = math.random(1,1900)
	local xnew = random/1000
	local new = game.ReplicatedStorage.ShowAdd:Clone()
	new.Parent = script.Parent.ScreenGui
	new.Position = UDim2.new(xnew,0,1,0)
	new.Text = "+"..players.leaderstats.Candy.Value - cp.."🍬"
	cp = players.leaderstats.Candy.Value
end)

it doesn’t work after the first two times the points changed.

It stops working after the first 2 times it gains points.