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

Is there any way to get a script to return to a certain line of code? (SOLVED)

Asked by 5 years ago
Edited 5 years ago

I've made this script that selects a random player to be a leader for each team in my game and it works fine, although I wanted to know if that upon a player leaving the game (specifically the leader) if there was a way to start the script over but avoid waiting 10 minutes again to select a new leader. I know how to use the PlayerRemoving event, but I don't know if there's a way to return to a line of code instead of starting the script over. For Example:

function IfLeftGerman(User)
   if User.Name == GermanLeader.Name then
   -- return to line of code 
end

game.Players.PlayerRemoving:Connect(IfLeftGerman)

This is my code right now:


local Players = game:GetService('Players') local pCount = Players:GetPlayers() while true do wait(600) -- Where I want the script to restart if the leader leaves if #pCount < 2 then print("Not Enough Players To Make Leader!") else French = game.Teams.French:GetPlayers() Germans = game.Teams.Prussians:GetPlayers() GermanLeader = (Germans[ math.random(#Germans)]) FrenchLeader = (French[ math.random(#French)]) Tag = Instance.new("NumberValue", GermanLeader.Character) Tag.Name = "LeaderTag" TagScript = game.ServerScriptService.Holder.TagScript:Clone() TagScript.Parent = Tag TagScript.Disabled = false Tag1 = Instance.new("NumberValue", FrenchLeader.Character) Tag1.Name = "LeaderTag" TagScript1 = game.ServerScriptService.Holder.TagScript:Clone() TagScript1.Parent = Tag TagScript1.Disabled = false -- PlayerRemoving event would go here end end

I believe I could still make the script work with another method, although it'd be a lot longer and messy. I want to know if there was a simple way to return to a certain line in a script.

1 answer

Log in to vote
1
Answered by
pidgey 548 Moderation Voter
5 years ago

Yes, the PlayerRemoving event would go there. When the playerremoving event fires, check if that player is the german leader. If it is, let the script know to start over. The script will know when to start over when it has finished waiting 600 seconds, OR if the german leader has left (you can just keep waiting in a while statement that checks both of these statements)

while true do
    if #game.Players:GetPlayers() >= 2 then --if the amount of players in this game is 2 or more
        --pick new players
        French = game.Teams.French:GetPlayers()
        Germans = game.Teams.Prussians:GetPlayers()

        GermanLeader = (Germans[ math.random(#Germans)])
        FrenchLeader = (French[ math.random(#French)])

        Tag = Instance.new("NumberValue", GermanLeader.Character)
        Tag.Name = "LeaderTag"
        TagScript = game.ServerScriptService.Holder.TagScript:Clone()
        TagScript.Parent = Tag
        TagScript.Disabled = false

        Tag1 = Instance.new("NumberValue", FrenchLeader.Character)
        Tag1.Name = "LeaderTag"
        TagScript1 = game.ServerScriptService.Holder.TagScript:Clone()
        TagScript1.Parent = Tag
        TagScript1.Disabled = false

        local left = false
        game.Players.PlayerRemoving:Connect(function(player) --when a player left
            if player ~= GermanLeader then --if the player that left is not the german leader, then
                return --you shall not pass
            end

            left = true --this player has left, let the loop know
        end)

        local timeThis = os.time()
        while (os.time() - timeThis) < 600 and left == false do --while this amount of seconds have passed is less than 600 and the player hasnt left yet,
            wait() --keep waiting
        end
    --we are done waiting, the loop will start over now
    end

    wait()
end

I have commented on what I edited.

0
It worked! Thank you so much, this cut the code down about 40 lines. NoahsRebels 99 — 5y
Ad

Answer this question