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

My for i, v in pairs loop isn't working for some reason for my finish line script. Why is this?

When I remove the loop the GUI displays and the wins value goes up, I'm trying to stop players from touching the end brick multiple times to get infinite wins. However nothing happens when I add the loop back. Why is this?

This is inside the end brick.

local Finish = script.Parent

Winners = {}

Finish.Touched:Connect(function(touch)
	for i, v in pairs (Winners) do
		if v == touch.Parent.Name then
			--oof, you've already finished
			
		else
			if touch.Parent.Humanoid then
				
				
				local touchParent = touch.Parent.Name
				
				local Player = game.Players:FindFirstChild(touch.Parent.Name)
				Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value + 1
				table.insert(Winners, Player.Name)
				
				game.ReplicatedStorage.PlayerWins:FireAllClients(touchParent)

			end
		end
	end

end)

1 answer

First, when using tables you should use ipairs, not pairs.(Refer to this)

Second, there's an easier way to get the player if they touch the part.

Here's what your code should look like:

local Finish = script.Parent
local Players = game:GetService("Players")

Winners = {}

Finish.Touched:Connect(function(touch)
    for i, v in ipairs(Winners) do
        if v == touch.Parent.Name then
            --oof, you've already finished

        else
            if touch.Parent.Humanoid then

                local Player = Players:GetPlayerFromCharacter(touch.Parent)
                Player.leaderstats.Wins.Value = Player.leaderstats.Wins.Value + 1
                table.insert(Winners, Player.Name)

                game.ReplicatedStorage.PlayerWins:FireAllClients(Player.Name)

            end
        end
    end

end)

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