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

How do I make a round end when the core is destroyed?

Asked by 2 years ago
Edited 2 years ago

I'm making a core defense game and I'm having problems. I want the loop to return to the beggining when the core is destroyed and I also want the round timer to stop, I've looked but cant find the solution to any.

RoundManager ServerScriptService ServerScript

local status = game.ReplicatedStorage.Status
local players = script.Players
local maps = game.ServerStorage.Maps:GetChildren()
local Spectating = game.Teams["Lobby/Spectating"]
local DTeam = game.Teams.Defenders
local RTeam = game.Teams.Raiders
local MorphRoomD = game.Workspace.MorphRoomD
local MorphRoomR = game.Workspace.MorphRoomR
local playersneeded = 1
local roundlength = 500
local intermissionlength = 30

local function checkplayers()
    if players.Value >= playersneeded then
        return true
    else
        return false
    end
end

local function IntermissionTimer()
    for i = intermissionlength, 0, -1 do
        wait(1)
        status.Value = "Intermission: "..i.." seconds left!"
    end
end

local function RoundTimer()
    for i = roundlength, 0, -1 do
        wait(1)
        status.Value = "Round In Progress: "..i.." seconds left!"
    end
end


local function Map()
    local randomchosenmap = maps[math.random(1,#maps)]
    status.Value = "Map Chosen!"
    wait(1)
    status.Value = "The Chosen map will be "..randomchosenmap.Name.."!"
    mapclone = randomchosenmap:Clone()
    mapclone.Parent = workspace
    core = mapclone.Core

    local morphRoomSpawnsD = MorphRoomD.Spawns:GetChildren()
    local morphRoomSpawnsR = MorphRoomR.Spawns:GetChildren()

    for i,player in pairs(game.Players:GetPlayers()) do
        if player.Team == DTeam then
            local char = player.Character or player.CharacterAdded:Wait()
            local RandomMorphRoomSpawnD = morphRoomSpawnsD[math.random(1,#morphRoomSpawnsD)]
            char.HumanoidRootPart.CFrame = RandomMorphRoomSpawnD.CFrame
        elseif player.Team == RTeam then
            local char = player.Character or player.CharacterAdded:Wait()
            local RandomMorphRoomSpawnR = morphRoomSpawnsR[math.random(1,#morphRoomSpawnsR)]
            char.HumanoidRootPart.CFrame = RandomMorphRoomSpawnR.CFrame
        end
    end
end

local function destroyMap()
    mapclone:Destroy()
end

local function reloadAllPlayers()
    for i,player in pairs(game.Players:GetPlayers()) do
        player:LoadCharacter()
    end
end

while wait() do
    if checkplayers() then
        IntermissionTimer()
        Map()
        RoundTimer()
        reloadAllPlayers()
        destroyMap()
    end
end

How would I implement that into my script?

2 answers

Log in to vote
1
Answered by 2 years ago
Edited 2 years ago

It is actually very simple. You just need to use the "continue" keyword at appropriate line of your script.

It looks something like:

for i = 0, 10, 1 do
    if (i == 5) then continue end -- It will not print 5
    print(i)
end

Edit: As for the round timer, you just need to make use of the "break" keyword at appropriate line of your script as well.

0
This is better than mine, please accept this one instead! NarwhalAndMe 141 — 2y
Ad
Log in to vote
0
Answered by 2 years ago

If the core was destroyed, you would fire an event to end the round something like this might work

local function RoundTimer()
    for i = roundlength, 0, -1 do
        wait(1)
        status.Value = "Round In Progress: "..i.." seconds left!"
    if Core == nil then
        break
    end
    end
end

Please tell me if this doesn't work or if someone notices a flaw!

0
I'll test it, I don't think it will work as timer is before core spawns but ill try with global variable! KamKam_AJHasBeenBan 37 — 2y
0
I also want to be able to use continue on the last while loop so ill try returning it. KamKam_AJHasBeenBan 37 — 2y
0
@NharwhalAndMe it only works in maps script for me so i just need to break round timer and continue last loop KamKam_AJHasBeenBan 37 — 2y

Answer this question