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.
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
Ensure you don’t have other scripts deleting your leaderstats and/or their contents.
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).
After fixing, check the explorer if these Instances actually exists inside the player.
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.
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.