Leaderstats not loading in properly?

Sometimes, not all the time, when a player loads in, their leaderstats won’t load in, making multiple scripts in the game go into a “WaitForChild” loop finding the leaderstat, though the leaderstat is 100% there. It’s game breaking and I haven’t figured ways out to fix it.

What im trying to accomplish is to 100% fix this error and not have the infinite yields and to get the data for the scripts asking for it.

my output when this error occurs

Show your code, specifically where it creates the leaderstats.

game.Players.PlayerAdded:Connect(function(Player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = Player
	
	local Brainrot = Instance.new("IntValue")
	Brainrot.Name = "Brainrot"
	Brainrot.Value = 0
	Brainrot.Parent = leaderstats
	
	local Rebirths = Instance.new("IntValue")
	Rebirths.Name = "Rebirths"
	Rebirths.Value = 0
	Rebirths.Parent = leaderstats
	
	local Multiplier = Instance.new("IntValue")
	Multiplier.Name = "Multiplier"
	Multiplier.Value = 0
	Multiplier.Parent = leaderstats
	
	local Price = Instance.new("IntValue")
	Price.Name = "Price"
	Price.Value = 50
	Price.Parent = leaderstats
	
	local RebirthCount = Instance.new("IntValue")
	RebirthCount.Name = "RebirthCount"
	RebirthCount.Value = 0
	RebirthCount.Parent = leaderstats
	
	local Gems = Instance.new("IntValue")
	Gems.Name = "Gems"
	Gems.Value = 0
	Gems.Parent = leaderstats
	
	local Level = Instance.new("IntValue",leaderstats)
	Level.Name = "Level"
	Level.Value = 1
	Level.Parent = leaderstats
	
	local Quests = Instance.new("IntValue",leaderstats)
	Quests.Name = "Quests"
	Quests.Value = 0
	Quests.Parent = leaderstats
	
	local Hatched = Instance.new("IntValue",leaderstats)
	Hatched.Name = "Hatched"
	Hatched.Value = 0
	Hatched.Parent = leaderstats
	
	local newPlayer = Instance.new("BoolValue",leaderstats)
	newPlayer.Name = "newPlayer"
	newPlayer.Value = 0
	newPlayer.Parent = leaderstats
	
	local BestTime = Instance.new("StringValue")
	BestTime.Name = "BestTime"
	BestTime.Value = 0
	BestTime.Parent = leaderstats

	local FloodEscapeWins = Instance.new("IntValue")
	FloodEscapeWins.Name =  "FloodWins"
	FloodEscapeWins.Value = 0
	FloodEscapeWins.Parent = leaderstats
	
	local Spins = Instance.new("IntValue")
	Spins.Name =  "Spins"
	Spins.Value = 3
	Spins.Parent = leaderstats
end)

the data gets saved btw, in a separate script.

  1. Consider using this pattern below. There is a possibility that the player is already in the server before the PlayerAdded signal connects, so we also loop through the already existing players manually.
local Players = game:GetService("Players")

local function onPlayerAdded(Player)
    -- code here
end

Players.PlayerAdded:Connect(onPlayerAdded)

for _, Player in Players:GetPlayers() do
    task.spawn(onPlayerAdded, Player)
end
  1. Ensure you don’t have other scripts deleting your leaderstats and/or their contents.
  2. Though it wont give any error, ensure you are following good coding habits, such as parenting after manipulating properties and assigning your Instance values to their proper types (you have a StringValue and BoolValue being set to a number).
  3. After fixing, check the explorer if these Instances actually exists inside the player.

maybe sometimes player join before this event connects

You also mention it saves in a separate script, but I’m assuming you removed the part where it loads the data? DataStoreService can fail, make sure you have fallback methods in case it does.

by this do you possibly mean

game:BindToClose(function()
	for _, v in pairs(Players:GetPlayers()) do
		Save(v)
	end
end)

I believe you fixed my error! I have tested over 10 times and haven’t received the errors I got originally thank you, and I will take this into my future of creating a large amount of stats.

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