Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

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

Asked by 3 years ago

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

Log in to vote
0
Answered by 3 years ago

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)
Ad

Answer this question