How to wait until the leaderstats data values have been loaded?

Hi basically I have a winstreak GUI that shouldnt be visible if its 0 but even if its not zero when the player joins and this script is ran I dont think the data for that player has been loaded yet so it thinks the players winstreak = 0 and just disables the GUI. How could I get around this?

game.Players.PlayerAdded:Connect(function(player) -- Fires whenever a player joins the game
	
	
	player.CharacterAdded:Connect(function(char) -- fires when the players character is added, 
		-- (this gets called not only when a player joins but also after they die)
		
		task.wait(1) -- I tried adding this to make it wait a bit but it only works sometimes (depending on how fast the data is loaded)
		
		if player:GetAttribute("InGame") == false then -- Checks that the player isnt currently in a game
			local winstreakGuiClone = winstreakGui:Clone() -- clones the GUI template
			winstreakGuiClone.Parent = char.Head -- parents it to the players characters head
			winstreakGuiClone.TextLabel.Text = tostring(player.storage:WaitForChild("Winstreak").Value) -- sets text to winstreak value
			if player.storage:WaitForChild("Winstreak").Value ~= 0 then -- checks if value = 0
				winstreakGuiClone.Enabled = true -- enables GUI
			else -- if not = 0
				winstreakGuiClone.Enabled = false -- disables GUI
			end
		end
	end)

You could add a line like :

player.storage:WaitForChild("Winstreak"):GetPropertyChangedSignal("Value"):Connect(function()
    if player.storage.Winstreak.Value ~= 0 then
        winstreakGuiClone.Enabled = true
    else
        winstreakGuiClone.Enabled = false
    end
end)
1 Like

seem like you are trying to clone a TextLabel to a player head which is not how it work. gui elements only work in LocalPlayer.PlayerGui.

to create a working textlabel, first create an instance called ScreenGui. then create a TextLabel inside the ScreenGui. ScreenGui displays descendants inside it when it is in PlayerGui.

if you want the gui to works when the game starts, move the ScreenGui to game.StarterGui. ScreenGuis will be automatically cloned into PlayerGui whenever a player join.
if you want the gui to works for later, move it to somewhere like ReplicatedStorage. then clone them and parent it to LocalPlayer.PlayerGui

That is not true, there are floating GUIs like BillBoard GUI (BillboardGui | Documentation - Roblox Creator Hub) which will make it possible to have some kind of 3D GUI in space. You can also parent it to any Instance in the workspace, even a player’s head. I would also try to set the Adornee of the GUI to the Instance too (because it’s meant for that).
The post I made earlier should fix your problem @rafferty05

1 Like

i thought he was using the display textlabel. sorry lol.

1 Like

Amazing thanks, yes this makes sense I’d imagine it will work Ill try it out in a bit

you have two solutions

solution 1:
just do

game.Players.PlayerAdded:Connect(function(player) -- Fires whenever a player joins the game
	
	
	player.CharacterAdded:Connect(function(char) -- fires when the players character is added, 
		-- (this gets called not only when a player joins but also after they die)
		
        repeat wait() until player:FindFirstChild("leaderstats")
		
		if player:GetAttribute("InGame") == false then -- Checks that the player isnt currently in a game
			local winstreakGuiClone = winstreakGui:Clone() -- clones the GUI template
			winstreakGuiClone.Parent = char.Head -- parents it to the players characters head
			winstreakGuiClone.TextLabel.Text = tostring(player.storage:WaitForChild("Winstreak").Value) -- sets text to winstreak value
			if player.storage:WaitForChild("Winstreak").Value ~= 0 then -- checks if value = 0
				winstreakGuiClone.Enabled = true -- enables GUI
			else -- if not = 0
				winstreakGuiClone.Enabled = false -- disables GUI
			end
		end
	end)

or solution 2

just make a remote event that is from the original data store script, and fire it when the datastore loaded is sucessful, this solution will guarente you that all the data has been loaded unlike the first solution where it only stops the loop if leaderstats has been created not when the data has been loaded

so that would be like this

game.ReplicatedStorage.leaderstatsLoaded:Connect(function(plr:Player) -- the :Player is a variable definer so it makes the script knwo that the plr is a player
	wait() -- just a wait id reccomend you dont remove it unless it bothers you lol

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

	if player:GetAttribute("InGame") == false then -- Checks that the player isnt currently in a game
		local winstreakGuiClone = winstreakGui:Clone() -- clones the GUI template
		winstreakGuiClone.Parent = char.Head -- parents it to the players characters head
		winstreakGuiClone.TextLabel.Text = tostring(player.storage:WaitForChild("Winstreak").Value) -- sets text to winstreak value
		if player.storage:WaitForChild("Winstreak").Value ~= 0 then -- checks if value = 0
			winstreakGuiClone.Enabled = true -- enables GUI
		else -- if not = 0
			winstreakGuiClone.Enabled = false -- disables GUI
		end
	end
end)
1 Like

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