.Changed not working for some reason

If you do say that It only doesn’t change on load up then it’s probably due to the script loading after the player instances have been loaded and registered the first values. In this case you might want to do this :

local player = game.Players.LocalPlayer

local function changeCoinsText(val)
    CoinsDisplay.Text = val 
end

player:WaitForChild(“Values”):WaitForChild(“Coins”).Changed:Connect(changeCoinsText)
changeCoinsDisplay()

I am currently on mobile so might have skipped some variables like coinsdisplay or gems but you probs understand what I mean.

1 Like

On Server
image

game.Players.PlayerAdded:Connect(function(player)
	
	local coins = Instance.new("NumberValue")
	coins.Name = "Coins"
	coins.Parent = player
	
	--pretend read from data store
	coins.Value = 100
	
	task.wait(10) --some game play happens
	
	--we get more coins
	coins.Value = 200
	
end)

On Client
image

local player = game.Players.LocalPlayer
local coins = player:WaitForChild("Coins")
--setup our changed event
coins.Changed:Connect(function()
	print("set changed coins",coins.Value)
end)

--init our gui to show what is in coins NOW
print("set starting coins ",coins.Value)

Gives this output

image

1 Like

image

It works like yours but the problem is that for some reason it doesn’t track the changes in value:


Player.Values.Coins.Changed:Connect(function(value)
	print("hi")
	CoinsDisplay.Text = Player.Values.Coins.Value
	
end)

no output

1 Like

Player.Values.Coins.Changed:Connect(function(value)
	print("hi")
	CoinsDisplay.Text = Player.Values.Coins.Value

end)

--init the gui with the current value of Coins
CoinsDisplay.Text = Player.Values.Coins.Value

try that

2 Likes

Same issue still. I’ve tried your function method, but the values do not change on the playergui.

1 Like

Sorry I must call the function on load up like display()

1 Like

This worked. Thank you very much.

1 Like

And just to make something clear about the .CharacterAdded on local script, it does work, but it has to be under PlayerScripts

image

local Player = game.Players.LocalPlayer
Player.CharacterAdded:Connect(function()
	print("Character Added")
end)

will output

image

EDIT It would probably also work under the GUI, as long as the GUI is created when the player loads and not when the character loads. There is an option to reset the gui or not when the character resets, this would need to be set to not reset the gui when the character resets.

1 Like

Both have pros and cons but for instances like StringValue and IntValue, Changed even is fired when the Instance’s value is changed. Using Changed could be better for IntValue

2 Likes

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