Why is my part only adding a "Win" leaderstat to the first person it touched and not everybody?

I am making a survival game and at the end of my game the players have to escape on a plane to safety and I added a invisible part that the plane flies through and it is supposed to give a “Win” to everybody in the plane but it only adds a win to the first player it touched here is the code:

local Part = script.Parent
local CanGet = true

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and CanGet then
			CanGet = false
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
			wait(3)
			CanGet = true
		end
	end
end

Part.Touched:Connect(onTouch)

I want this to give a Win to everybody that is in the plane but it just gives one to the first person in the plane, Thanks.

you could do a for i, v in pairs loop with the players to give the value.

local Part = script.Parent
local CanGet = true

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and CanGet then
			CanGet = false
			for i, plr in pairs (game.Players:GetChildren()) do
				plr.leaderstats.Wins.Value = plr.leaderstats.Wins.Value + 1
			end
			wait(3)
			CanGet = true
		end
	end
end

Part.Touched:Connect(onTouch)
1 Like

this sounds dumb, but maybe because your disablng CanGet when touched, not allowing others to get it.

1 Like

Did the players that touch it after the first person gets it wait for the timer to go before CanGet is true?

Also you can just get the player bu doing

local player = game.Players:GetPlayerFromCharacter(otherPart.Parent)

From my understanding, when I first read it, I thought you meant as in like a whole group gets the win if one player touches it. I don’t know if that is what you’re looking for or not.

If it is then:

You can add a bool value to each player to determine if the player is in the plane or not.

And from there you can use a for i, v in pairs loop for the first person who touches the part, so that everyone can get the win.

You can add a while true loop when calling Part.Touched

local Part = script.Parent
local CanGet = true

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and CanGet then
			CanGet = false
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
			wait(3)
			CanGet = true
		end
	end
end

while true do
	wait(0)
	Part.Touched:Connect(onTouch)
end

You are setting CanGet to false once a player touches it, it’s basically the debounce of your script. Maybe make it so you set a boolean (true/false) attribute to the player and set it to true for the player who touches it.

local Part = script.Parent

local function onTouch(otherPart)
	local humanoid = otherPart.Parent:FindFirstChild('Humanoid')
	if humanoid then
		local player = game.Players:FindFirstChild(otherPart.Parent.Name)
		if player and player:GetAttribute("CanGet") then -- Assuming player has an attribute "CanGet" and is set to true
			player:SetAttribute("CanGet", false) -- Set the attribute to false so next time they try to touch it, nothing happens.
			player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
			wait(3)
			CanGet = true
		end
	end
end

Part.Touched:Connect(onTouch)

Up to you how you’ll implement the CanGet attribute for each player. Since it seems you have an end to your game, make it so it gives players the CanGet attribute once the game starts.

In case you don’t know attributes yet: Instance Attributes