Leaderstats sometimes fail to load in upon joining game

I am making a game and I have 2 leaderstats, coins and level, and sometimes when I join the game, one of them does not load in and my stats do not appear on the player list. This only happens when you have 2 stats and it works fine with just one.
I have a script for each stat in the workspace
Script 1:

local DataStore = game:GetService("DataStoreService")
local ds = DataStore:GetDataStore("LevelSave")

game.Players.PlayerAdded:connect(function(player)
	local leader = Instance.new("Folder",player)
	leader.Name = "leaderstats"
	local Level = Instance.new("IntValue",leader)
	Level.Name = "Level"
	Level.Value = ds:GetAsync(player.UserId) or 1
	ds:SetAsync(player.UserId, Level.Value)
	Level.Changed:connect(function()
		ds:SetAsync(player.UserId, Level.Value)
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
	ds:SetAsync(player.UserId, player.leaderstats.Level.Value)
end)

Script 2:

local DataStore = game:GetService("DataStoreService")
local ds2 = DataStore:GetDataStore("CoinSave")

game.Players.PlayerAdded:connect(function(player)
	local leader = player.leaderstats
	leader.Name = "leaderstats"
	local Coins = Instance.new("IntValue",leader)
	Coins.Name = "Coins"
	Coins.Value = ds2:GetAsync(player.UserId) or 100
	ds2:SetAsync(player.UserId, Coins.Value)
	Coins.Changed:connect(function()
		ds2:SetAsync(player.UserId, Coins.Value)
	end)
end)


game.Players.PlayerRemoving:connect(function(player)
	ds2:SetAsync(player.UserId, player.leaderstats.Coins.Value)
end)

both scripts are making a folder called leaderstats.
there may only be 1 folder called leaderstats

nvm let me look again
in your 2nd script instead of

   local leader = player.leaderstats
   leader.Name = "leaderstats"

do

local leader = player:WaitForChild("leaderstats")
1 Like