Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Why does it change all the players leaderstats?

So when I join my game, and touch a block it changes my leaderstats called "Level". But when I test it out with multiple people, and I touch the block. It changes all the people leaderstats to given value. How can I fix this?

game.Players.PlayerAdded:Connect(function(player)
	local debounce = true
	script.Parent.Touched:Connect(function()
		local plrstats = player.leaderstats.Level
		if debounce then
			debounce = false
			plrstats.Value = 1
			wait (1)
			debounce = true
		end
	end)
end)

2 comments

0

You are creating a .Touched event everytime a player joins, and plus you didn't specify which player touch the brick, so 1 player touch the brick, everyone's leaderstats will be changed

0

so how would I fix this?

1 answer

✓ Accepted answer

Because the parameter for game.Players.PlayerAdded:Connect(function() is player, at which the player parameter is connected to all the players who joined the game. You should base your script on the touched function, which returns individual parts that touched the part. So, your script would be:

local debounce = true

script.Parent.Touched:Connect(function(partThatTouched)
	local humanoid = partThatTouched.Parent:FindFirstChildWhichIsA("Humanoid") --Humanoid
	if humanoid then --Checks for humanoid
		local player = game.Players:FindFirstChild(partThatTouched.Parent.Name) --Finds player
		local plrstats = player.leaderstats.Level
		if debounce then
			debounce = false
			plrstats.Value = 1
			wait (1)
			debounce = true
		end
	end	
end)

2 comments

0

Thanks yous very much

0

Welcome!

This discussion is preserved as it was. New answers, comments, and votes are closed.