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:

1function IfLeftGerman(User)
2   if User.Name == GermanLeader.Name then
3   -- return to line of code
4end
5 
6game.Players.PlayerRemoving:Connect(IfLeftGerman)

This is my code right now:

01local Players = game:GetService('Players')
02local pCount = Players:GetPlayers()
03 
04while true do
05    wait(600)
06 
07   -- Where I want the script to restart if the leader leaves
08 
09    if #pCount < 2 then
10    print("Not Enough Players To Make Leader!")
11    else
12 
13    French = game.Teams.French:GetPlayers()
14    Germans = game.Teams.Prussians:GetPlayers()
15 
View all 34 lines...

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)

01while true do
02    if #game.Players:GetPlayers() >= 2 then --if the amount of players in this game is 2 or more
03        --pick new players
04        French = game.Teams.French:GetPlayers()
05        Germans = game.Teams.Prussians:GetPlayers()
06 
07        GermanLeader = (Germans[ math.random(#Germans)])
08        FrenchLeader = (French[ math.random(#French)])
09 
10        Tag = Instance.new("NumberValue", GermanLeader.Character)
11        Tag.Name = "LeaderTag"
12        TagScript = game.ServerScriptService.Holder.TagScript:Clone()
13        TagScript.Parent = Tag
14        TagScript.Disabled = false
15 
View all 39 lines...

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