游戏回合后重置脚本?

Resetting a script after game round?

当游戏计时器结束时,它会杀死玩家并重置团队并将他们发送到出生点以再次选择一个团队...我知道如何重置脚本以从头开始并重置所有值和函数称为...我尝试制作脚本的副本并使用 script:Destroy() 销毁当前脚本但不起作用并继续使用相同的功能因此当玩家再次选择团队并重生时我的游戏中断了。

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

您可以将脚本包装在 while loop 中,以便在回合结束时从头开始重复。在循环结束时,就在 end 标记之前,您可以重置下一轮应该重置的所有值。