Simple LocalScript not running?

At first I made a more complex script but after numerous attempts of it not working I commited to a simpler one, and yet it doesn’t work.
This is the script.

local rs = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer

plr.CharacterAdded:Connect(function(char)
	local Coins = plr:WaitForChild("leaderstats").Coins
	local UI = plr.PlayerGui.MenuUi
	local coinstxt = UI.MenuFrame.Coins.CoinLabel
	
	coinstxt.Text = "hi"
end)

What could be wrong?

1 Like

Can you show the hierarchy of the guis?

1 Like

image

1 Like

sorry wrong code try this:

local rs = game:GetService("ReplicatedStorage")
local plr = game.Players.LocalPlayer

local char = plr.Character or plr.CharacterAdded:Wait()

  if char then
	local Coins = plr:WaitForChild("leaderstats").Coins
	local UI = plr.PlayerGui.MenuUi
	local coinstxt = UI.MenuFrame.Coins.CoinLabel

	coinstxt.Text = "hi"
	
	end



this should work

2 Likes

So prints also dont work directly after CharacterAdded? Could be also the cause of WaitForChild being stuck in a yield so the Text line isnt running. Also, you could try using Humanoid.Died.

1 Like

Found this article called CharacterAdded. I tried the code myself, and it did work, but only if the code was in StarterPlayerScripts.

1 Like

For the client, your character has likely loaded before you can even connect .CharacterAdded event. This is just the case – this is why you often use CharacterAdded directly under PlayerAdded.

To fix it, you can do @zahra_y735 's suggestion by doing plr.Character or plr.CharacterAdded:Wait()
Or you can just make a separate function → call it for the first time upon player join → connect it to CharacterAdded event

1 Like

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