Search code examples
luaresetroblox

Resetting a script after game round?


When the game timer ends it kills the players & resets the teams and sends them to spawn to choose a Team again... idk how to reset the script to start from the beginning and have reset all the values and functions called... I tried making a copy of the script and destroy the current one with script:Destroy() but doesn't work & continues with the same function so breaks my game when the players choose the teams again & respawn.

-- Get Service Variables
local Teams = game:GetService("Teams")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local plr = game:GetService("Players").LocalPlayer

-- Wait for Child Variables
local TeamResetter = game.ReplicatedStorage.TeamResetter
local TimeCountdown = ReplicatedStorage:WaitForChild("Timer")


--Scripts Resets the entire script after GameTime is up
local function ResetGame(Player,Teams)
    local copy = script:Clone()
    copy.Parent = script.Parent
    script:Destroy()
end


-- Destroy Gate when thieves touch it
game.Workspace.CarGate4.GateRod.Touched:Connect(function(hit,Player)
    local h = hit.Parent:FindFirstChild("Humanoid")
    if h ~= nil then
        local n = hit.Parent
        local p = game.Players:FindFirstChild(n.Name)
        if p.Team.Name == "Thieves" then
            game.Workspace.CarGate4.GateRod:Destroy()
        end
    end         
end)


--Thieves function for winning
--[[humanoid.Seated:Connect(function(active,currentSeat, Player, Team)
    if active then
        if currentSeat.Name == "DriveSeat" then
            if Player.TeamColor == game.Teams.Thieves.TeamColor then
                game.StarterGui.ThiefWinScreen.Frame.TextLabel.Script.Disabled = false
        end

    end

end
end)]]


local function PoliceWinReset(Player,Team)
    game.StarterGui.PoliceWinScreen.Frame.TextLabel.Script.Disabled = false
    wait(2)
    for i,v in pairs(game.Teams.Thieves:GetPlayers()) do
        Player.TeamColor = game.Teams.ChoosingTeam.TeamColor
        game.StarterGui.ThiefWinScreen.Frame.TextLabel.KillPlayer.Disabled = false
        Player.Character:BreakJoints()
        game.StarterGui.ChooseTeamGUI.Enabled = true
        ResetGame(Player,Teams)
    end
    for i,v in pairs(game.Teams.Police:GetPlayers()) do
        Player.TeamColor = game.Teams.ChoosingTeam.TeamColor
        game.StarterGui.PoliceWinScreen.Frame.TextLabel.KillPlayer.Disabled = false
        Player.Character:BreakJoints()
        game.StarterGui.ChooseTeamGUI.Enabled = true
        ResetGame(Player,Teams)
end
end

--Starts Global timer for game after user chooses a team & Police win code
--Resets Player Teams and respawns them back at spawn and have to choose a team again
local function PlayGame(Player, Team)
    local timerAmount = 120
    local timerText = ""
    while timerAmount >= 0 do
        TimeCountdown:FireAllClients(timerAmount,timerText)
        wait(1)
        timerAmount -= 1
        if timerAmount == 0 then
            PoliceWinReset(Player,Team)
        end
    end
    return timerAmount
end
--Checks wether the user is on the Thieves or Police Teama
local function Thieves_Police(Player, Team)
    if Player.TeamColor == game.Teams.Police.TeamColor then
        game.StarterGui.ChooseTeamGUI.Enabled = false
        game.StarterGui.TimerGUI.Enabled = true
        wait(5)
        PlayGame(Player, Team)
        return Player, Team

    elseif Player.TeamColor == game.Teams.Thieves.TeamColor then
        game.StarterGui.ChooseTeamGUI.Enabled = false
        game.StarterGui.TimerGUI.Enabled = true
        wait(5)
        PlayGame(Player, Team)
        return Player, Team
    end
end
--Team Chooser
game.ReplicatedStorage.TeamChooser.OnServerEvent:Connect(function(Player, Team)
    local PhysicalTeamColor = Teams:FindFirstChild(Team).TeamColor
    Player.TeamColor = PhysicalTeamColor
    game.StarterGui.PoliceWinScreen.Enabled = false
    game.StarterGui.ThiefWinScreen.Enabled = false
    Thieves_Police(Player, Team)
    
end)

--Gives the Users on the Police Team a Weapon on Spawn
function teamFromColor(color) 
    for _,t in pairs(game:GetService("Teams"):GetChildren()) do 
        if t.TeamColor==color then return t end 
    end 
    return nil 
end 


function onSpawned(plr) 
    local tools = teamFromColor(plr.TeamColor):GetChildren() 
    for _,c in pairs(tools) do 
        c:Clone().Parent = plr.Backpack 
    end 
end 

function onChanged(prop,plr) 
    if prop=="Character" then 
        onSpawned(plr) 
    end 
end 

function onAdded(plr) 
    plr.Changed:connect(function(prop) 
        onChanged(prop,plr) 
    end) 
end 



--Calls the Functions
game.Players.PlayerAdded:connect(onAdded)

Solution

  • You can just wrap the script in a while loop to repeat from the beginning when the round ends. At the end of the loop, right before the end tag, you can reset all the values that are supposed to be reset for the next round.