Every Player has the same leaderstats

Hello,
i want to make a script which makes, that all players have the same leaderstats.
This is my code:

hostplr = nil
_G.mainplr = nil

game.Players.PlayerAdded:Connect(function(plr)
	for i, player in pairs(game.Players:GetPlayers()) do
		if player:WaitForChild("savestats"):WaitForChild("LobbyRole").Value == "host" then
			_G.mainplr = player 
		else
			if player:WaitForChild("savestats"):WaitForChild("LobbyRole").Value == "member" then
				player:WaitForChild("leaderstats").MoneyOnHand.Value = _G.mainplr.leaderstats.MoneyOnHand.Value
			end
		end
	end
end)

It works, but when a normal player (not the host) loads in before the host, it doesn’t work anymore.
I hope someone can help me
~ maini

can u explain exactly what do u want?

1 Like

Can u show me whats script u put for choose the host?

1 Like

i want that every player has the same exactly money as the player who has the most money

oh ok but u have created the script for that right can u show me?

1 Like

So i save it with a GlobalDatastore, and then I teleport the player in the normal game. Here I choose the host:

			plr:WaitForChild("savestats"):WaitForChild("UserId").Value = plr.UserId
			plr.savestats.ModeSave.Value = "normal"
			
			Player.savestats.LobbyRole.Value = "member"
			
			plr.savestats.LobbyRole.Value = "host"

try that:

hostplr = nil
_G.mainplr = nil

function Check(player)
	if player:WaitForChild("savestats"):WaitForChild("LobbyRole").Value == "host" then
		_G.mainplr = player 
	else
		if _G.mainplr ~= nil then
			if player:WaitForChild("savestats"):WaitForChild("LobbyRole").Value == "member" then
				player:WaitForChild("leaderstats").MoneyOnHand.Value = _G.mainplr.leaderstats.MoneyOnHand.Value
			end
		end
	end
end

for i,v in pairs(game.Players:GetPlayers()) do
	Check(v)
end

game.Players.PlayerAdded:Connect(Check)
1 Like

Because you are using .PlayerAdded, if the game hasn’t loaded or the host is not in the game yet the stat _G.mainplr is never set.

What you should do instead is account for this issue and load when the data is present:

hostplr = nil
_G.mainplr = nil

function LoadData(player) 
	if player:WaitForChild("savestats"):WaitForChild("LobbyRole").Value == "host" then
		_G.mainplr = player 
		for _, n_player in ipairs(game.Players:GetPlayers()) do
			if n_player == player then return end
			LoadData(n_player) 
		end
	else
		if _G.mainplr ~= nil then
			if player:WaitForChild("savestats"):WaitForChild("LobbyRole").Value == "member" then
				player:WaitForChild("leaderstats").MoneyOnHand.Value = 	_G.mainplr.leaderstats.MoneyOnHand.Value
			end
		end
	end
end

game.Players.PlayerAdded:Connect(LoadData)

-- Load previous players
for _, n_player in ipairs(game.Players:GetPlayers()) do 
	LoadData(n_player) 
end

This way you can achieve it, whilst not having the issue of people loading previously.

1 Like

I get this error:
Script ‘ServerScriptService.Other.LoadMoney’, Line 9 - function LoadData

Oh wait one moment, its been made recursive when it shouldn’t be.

EDIT:
Should be amended, I haven’t tested it in studio however.

1 Like

Are you ready with the script?

I made the edit already to the one above.

Oh sorry, I didnt noticed it.

There is no error, but its still not working.

Hello? Could you please answer? That would be very kind.

I get a error now:

ServerScriptService.Other.LoadMoney:8: attempt to index nil with 'WaitForChild'

It says that player = nil

Hey, whats the full script?

Also I can’t help a ton as I don’t really use leaderstats to handle data.

What do you use to handle data normally?
i fixed it myself

I usually use a dictionary to store the stats, way more reliable then the leaderstats board and its also more secure - additionally, makes it easier on memory also. :slight_smile: