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

My Code Doesn't Repeat, at a Certain Part?

Asked by
Borrahh 265 Moderation Voter
3 years ago

The Idea is, when 2 players touch a part, they enter the table, Get teleported to the Parts, and then have a sword fight, which works fine for the first time. When they try to duel again, the code from

repeat wait() until #playersFighting == 2 do

to the end, doesn't work, I don't get any error, They don't get any swords, or get telepored, anyone knows why

local playersFighting = {}


workspace.PvpSystem.Part.Touched:Connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        if #playersFighting < 3 then
            if not table.find(playersFighting, hit.Parent) then
                table.insert(playersFighting, hit.Parent)
                print("Added Player: "..hit.Parent.Name.." to the Table!")
            else
                print("You are already in the table")
            end
        else
            print("There are already 2 members")
        end
    end
end)

repeat wait() until #playersFighting == 2 do
    playersFighting[1].HumanoidRootPart.Position = workspace.PvpSystem.player1TP.Position + Vector3.new(0, 6, 0)
    playersFighting[2].HumanoidRootPart.Position = workspace.PvpSystem.player2TP.Position + Vector3.new(0, 6, 0)
    -- game here
    for i, v in pairs(playersFighting) do
        local theSword = game.ServerStorage:WaitForChild("ClassicSword"):Clone()
        theSword.Parent = v



        v.Humanoid.Died:Connect(function(player)
            table.remove(playersFighting, i)
            --Reward
            print("The Winner is: "..v.Name)

            table.remove(playersFighting, i)
        end)
    end
end

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

Well that line is not written correctly...I do not think you can put "repeat until...do." If you're meaning to run the code when there are 2 players fighting, wouldn't it better to use "while true do"?? Or did you mean to use a "repeat until" loop?

while #playersFighting == 2 do
    wait()
    playersFighting[1].HumanoidRootPart.Position = workspace.PvpSystem.player1TP.Position + Vector3.new(0, 6, 0)
    playersFighting[2].HumanoidRootPart.Position = workspace.PvpSystem.player2TP.Position + Vector3.new(0, 6, 0)
    -- game here
    for i, v in pairs(playersFighting) do
        local theSword = game.ServerStorage:WaitForChild("ClassicSword"):Clone()
        theSword.Parent = v



        v.Humanoid.Died:Connect(function(player)
            table.remove(playersFighting, i)
            --Reward
            print("The Winner is: "..v.Name)

            table.remove(playersFighting, i)
        end)
    end
end
Ad

Answer this question