Leaderstats Problem

Hello! I’m trying to make a script that gives a point (or leaderstat value) to all the players in the game. I’ve tried different solutions, and this is the only one that I got remotely working.

I’ve tried this:

	local allplayers = game.Players:GetPlayers()
	for _, player in pairs(allplayers) do
		game.Players:FindFirstChild(player.Name).leaderstats.Wins.Value = game.Players:FindFirstChild(player.Name).leaderstats.Wins.Value + 1
	end

It works when there is one player in the game, but it does not work when there are multiple people in the game. If there are, say 2 people, then it would give each player 2 Wins. If there were 3 players, then each player would get 3 wins, and so on.

Thanks in advance! :grinning:

while wait(5) do
	local allplayers = game.Players:GetPlayers()
	for _, player in pairs(allplayers) do
		game.Players[player.Name].leaderstats.Coins.Value += 1
	end
end

The issue probably isn’t in this specific script, I’ve tried this with 3 players and it worked perfectly fine in Studio for me, all 3 players received +1 Value without any problems.

I also recommend using the += signal, and since Players:GetPlayers() returns the player you don’t need to do

game.Players:FindFirstChild(player.Name)

Instead you can just do this:

local allplayers = game.Players:GetPlayers()
	for _, player in pairs(allplayers) do
		player.leaderstats.Wins.Value += 1
	end

Feel free to post any other scripts that may be affecting this one.

I tried to change that code in it, and it still did the same thing. I’m pretty sure that it is coming from this script because I have it activating when you touch something.

This is the whole script:

local debounce = false
game.Workspace.EndPart.Touched:Connect(function(player)
	if debounce == false then
			debounce = true
			local allplayers = game.Players:GetPlayers()
			for _, player in pairs(allplayers) do
				player.leaderstats.Wins.Value += 1
			end
		end
end)

Should this be in a LocalScript?

no this should be in a normal script

1 Like

Sorry, but I still cannot reproduce this issue.

Just a reminder: Touched function does not return the player, but the part who touched, if you want it to fire only when the player touched you should do a check like so:

if hit.Parent:FindFirstChild("Humanoid") then

I’ve tested with this script:

local debounce = false

script.Parent.Touched:Connect(function(hit)
	if hit.Parent:FindFirstChild("Humanoid") then
		if debounce == false then
			debounce = true
			
			local allplayers = game.Players:GetPlayers()
			for _, player in pairs(allplayers) do
				player.leaderstats.Money.Value += 1
			end
		end
	end
end)

Result:

1 Like

Ok, so I got it to work, thanks for the help!

The problem was that the script was in StarterGui because I needed it there for some UI.

1 Like