Object won't load in time, despite adding :WaitForChild()

Title explains it all. I have player folders in ReplicatedStorage that are added when the player joins, these folders contain all of their data.
I also have a GUI in the bottom left corner of the screen that displays how many coins a player has, and the amount of coins are found in said playerFolder. I have made a simple script that updates the GUI when the coin amount updates.

local player = game.Players.LocalPlayer

local playerFolder = game.ReplicatedStorage.Players:WaitForChild(player.UserId) or game.ReplicatedStorage.Players[player.UserId]

script.Parent.Text = playerFolder.Coins.Value

playerFolder.Coins.Updated:Connect(function(value)
	print("more coins")
	script.Parent.Text = value
end)

An obvious issue is that this script runs the moment it loads into the client, so the player folder may not be there by the time the script starts and an error will be thrown, no biggie, I just add the condition game.ReplicatedStorage.Players:WaitForChild(player.UserId) to ensure that if it hasn’t already loaded, it will be set once it loads. But this does not work, I am still given errors saying that the userId of the player (which is the name of the playerFolder) is not found in ReplicatedStorage when the script runs. Does the script not halt everything that happens until that child is added or detected? How do I fix this?

can you try to rename the player id to a player name and check this on error?

If this error still appear can i see a script that adds game.ReplicatedStorage.Players player id?

1 Like
players.PlayerAdded:Connect(function(player)
	local invAsync = inventoryStore:GetAsync(player.UserId) -- {inventory, equipped, coins}
	-- inventory stable is structured as {{1, 1}, {2, 4}...} etc with 1 being weapons, 2 being pets.
	local bonkAsync = bonkStore:GetAsync(player.UserId) -- simple value
	local killAsync = killStore:GetAsync(player.UserId) -- also simple value
	local playerFolder = script.PlayerFolder:Clone()
	playerFolder.Name = player.UserId
	playerFolder.Parent = game.ReplicatedStorage.Players

It is a very long script, but this is the part that adds the playerFolder.

Edit: Solved the issue. Updated is the wrong word, I am supposed to use Changed.