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

Is there a way to cancel a function while it is running?

Asked by
Vain_p 78
5 years ago

So I want to cancel this function so when all the players die but how can I cancel a function tho?

function StartGame()
    ChooseGame()
    print(rm)
    game.ReplicatedStorage.PlayersAlive.Value = game.ReplicatedStorage.NumPlayers.Value
    for i,v in pairs(game.Players:GetChildren()) do
        v.Backpack.inGame.Value = "Game"
    end
    game.ReplicatedStorage.GameStatus.Value = "Game"
    if rm == 1 then
        game.ReplicatedStorage.GameStatus.Value = "Game"
        local WaterRunMap = game.ServerStorage.WaterRun:Clone()
        WaterRunMap.Parent = game.Workspace
        game:GetService("ReplicatedStorage").GameTime60:Fire()
        game:GetService("ReplicatedStorage").WaterRun:FireAllClients()
        wait(2.5)
        for i,v in pairs(game.Players:GetChildren()) do
            v.Character.HumanoidRootPart.CFrame = CFrame.new(-207.5, 17.5, 151.5)
        end
        for t = 60,0,-1 do
            game.ReplicatedStorage.GameTime.Value = t
            wait(1)
        end
        EndGame()
        Intermission()
    elseif rm == 2 then
        game.ReplicatedStorage.GameStatus.Value = "Game"
        local BombBombRainMap = game.ServerStorage.BombBombRain:Clone()
        BombBombRainMap.Parent = game.Workspace
        game.ServerScriptService.BombRain.Disabled = false
        game:GetService("ReplicatedStorage").GameTime60:Fire()
        game:GetService("ReplicatedStorage").BombBombRain:FireAllClients()
        wait(2.5)
        for i,v in pairs(game.Players:GetChildren()) do
        v.Character.HumanoidRootPart.CFrame = CFrame.new(-43, 2.5, 27)
        end
        for t = 60,0,-1 do
            game.ReplicatedStorage.GameTime.Value = t
            wait(1)
        end
        game.ServerScriptService.BombRain.Disabled = true
        EndGame()
        Intermission()
    elseif rm == 3 then
        game.ReplicatedStorage.PlayersAlive.Value = game.ReplicatedStorage.NumPlayers.Value
        game.ReplicatedStorage.GameStatus.Value = "Game"
        game:GetService("ReplicatedStorage").GameTime120:Fire()
        local SwordFightMap = game.ServerStorage.SwordFight:Clone()
        SwordFightMap.Parent = game.Workspace
        game:GetService("ReplicatedStorage").SwordFight:FireAllClients()
        wait(2.5)
        for i,v in pairs(game.Players:GetChildren()) do
        v.Character.HumanoidRootPart.CFrame = CFrame.new(2, 5, 80)
        end
        for t = 120,0,-1 do
            game.ReplicatedStorage.GameTime.Value = t
            wait(1)
        end
        EndGame()
        Intermission()
    --[[elseif rm == 4 then
        local PlatformRunMap = game.ServerStorage.PlatformRun:Clone()
        PlatformRunMap.Parent = game.Workspace
        if NumPlayers.Value == 2 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 3 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 4 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 5 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 6 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 7 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 8 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 9 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 10 then
            print("PlaceHolder")
        elseif NumPlayers.Value == 11 then -- Sometimes roblox's servers can go 1 player over max So this is for that player 
            print("PlaceHolder")
        elseif NumPlayers.Value == 1 then
            print("Not Enough Players for gamemode!")
            print("Ending game!")
            PlatformRunMap:Destroy()
        end
        --]]
        elseif rm == 4 then --This Map is disabled Because it is a wip
        game.ReplicatedStorage.PlayersAlive.Value = game.ReplicatedStorage.NumPlayers.Value
        game.ReplicatedStorage.GameStatus.Value = "Game"
        local LaserTagMap = game.ServerStorage.LaserTag:Clone()
        LaserTagMap.Parent = game.Workspace
        wait(2.5)
        for i,v in pairs(game.Players:GetChildren()) do
            v.Character.HumanoidRootPart.CFrame = CFrame.new(104, 7.59996033, -35)
        end
        game:GetService("ReplicatedStorage").LaserTag:FireAllClients()
        wait(10)
        for t = 120,0,-1 do
            game.ReplicatedStorage.GameTime.Value = t
            wait(1)
        end
        EndGame()
        Intermission()
    elseif rm == 6 then --This Map is disabled Because it is a wip
        local SewersMap = game.ServerStorage.WIPSewers:Clone()
        SewersMap.Parent = game.Workspace
    end
end

I want to cancel this function

0
Use the keyword 'return' to prematurely end a function, or return a value from it, or both User#24403 69 — 5y
0
I don't know if i'm right or not, but try using the "Break" function. B_rnz 171 — 5y
0
break is used for loops, return is used to end a function. AbstractionsReality 98 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago

I believe you call 'return'. This would make the function return a value of 'nil', which would cancel your function as a side effect.

0
how would I do that cause I am trying to cancel it via another function? Vain_p 78 — 5y
0
While I don't really know exactly how to do that, I think the answer is in coroutines. You start the function you want to be cancelable via coroutine, and then when you want to cancel it, the other function can call some coroutine function to stop the coroutine. DoubleDual36 18 — 5y
0
so I use local StartGame = coroutine.create (function() and put the function inside of it? Vain_p 78 — 5y
0
what do I do after that? Vain_p 78 — 5y
0
Not sure looool DoubleDual36 18 — 5y
Ad

Answer this question