Ad
Log in to vote
0

How can you break a while loop when a part is touched?

Asked by
MattVSNNL 187
7 days ago
Edited 6 days ago

I'm making a small basic game and I want to add if a player touches the end they win so in the round system I added a code to detect if the part is touched and if the player that touched it was in the player's table then the while loop breaks, But when I type break, It has a red line under it meaning it doesn't know the while loop, Can anyone help?

Touch code: Tell me if you need the whole code

        clonedMap:FindFirstChild("EndPart").Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
                if table.find(plrs, player) then
                    status.Value = player.Name.." has won the game!"
                    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + reward
                    player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
                end
            end
        end)

All the code

local replicatedStorage = game:GetService("ReplicatedStorage")
local status = replicatedStorage:FindFirstChild("Status")
local intermissionLength = 10
local gameLength = 120
local mapsFolder = replicatedStorage:FindFirstChild("Maps")
local reward = 5

while true do
    status.Value = "Waiting for enough players to start!"

    repeat wait(1) until game:GetService("Players").NumPlayers >= 1

    for i = intermissionLength,0,-1 do
        wait(1)
        status.Value = "Intermission: "..i
    end

    local plrs = {}

    for i, player in pairs(game:GetService("Players"):GetPlayers()) do
        if player then
            table.insert(plrs, player)
        end
    end

    local AvailableMaps = mapsFolder:GetChildren()

    local randomMap = AvailableMaps[math.random(1,#AvailableMaps)]

    local clonedMap = randomMap:Clone()

    status.Value = randomMap.Name.." has been selected as next map"

    wait(1.25)

    clonedMap.Parent = workspace

    wait(2)

    local SpawnPoints = clonedMap:FindFirstChild("SpawnPoints")

    if not SpawnPoints then
        print("No spawn points found!")
    end

    local AvailableSpawnPoints = SpawnPoints:GetChildren()

    for i, player in pairs(plrs) do
        if player then
            character = player.Character

            if character then
                character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
                table.remove(AvailableSpawnPoints,1)

                local GameTag = Instance.new("BoolValue")
                GameTag.Name = "GameTag"
                GameTag.Parent = player.Character

            else
                if not player then
                    table.remove(plrs, player)
                end
            end
        end
    end

    replicatedStorage:FindFirstChild("DisableControls"):FireAllClients()

    wait(1)

    status.Value = "Get ready to play"

    wait(2)

    replicatedStorage:FindFirstChild("EnableControls"):FireAllClients()

    for i = gameLength,0,-1 do

        for x, player in pairs(plrs) do
            if player then

                character = player.Character

                if not character then
                    -- Left the game or rage quited lol
                    table.remove(plrs,x)
                else
                    if character:FindFirstChild("GameTag") then
                        -- They are sill alive
                        print(player.Name.." Is still here")
                    else
                        -- Dead or rage quited lol
                        table.remove(plrs,x)
                        print(player.Name.." has been removed!")
                    end
                end
            else
                table.remove(plrs,x)
                print(player.Name.." has been removed!")
            end
        end

        status.Value = i.." Seconds Left, And "..#plrs.." Players left"

        clonedMap:FindFirstChild("EndPart").Touched:Connect(function(hit)
            if hit.Parent:FindFirstChild("Humanoid") then
                local player = game:GetService("Players"):GetPlayerFromCharacter(hit.Parent)
                if table.find(plrs, player) then
                    status.Value = player.Name.." has won the game!"
                    player.leaderstats.Coins.Value = player.leaderstats.Coins.Value + reward
                    player.leaderstats.Wins.Value = player.leaderstats.Wins.Value + 1
                end
            end
        end)

        wait(1)
    end

    print("End of game")

    wait(5)

    for i, player in pairs(game.Players:GetPlayers()) do
        character = player.Character

        if not character then
            -- Ignore em
        else
            if character:FindFirstChild("GameTag") then
                character.GameTag:Destroy()
            end
        end

        player:LoadCharacter()
    end

    clonedMap:Destroy()

    status.Value = "This Round has Ended"

    wait(2)
end
0
I'll need the whole code to make that. NotConnorRandumb 2 — 7d
0
You don't actually need to use break to stop a while loop. You could change the condition for the loop, like while something do. And then when the EndPart is touched you change that value to false. Spjureeedd 363 — 6d

1 answer

Log in to vote
0
Answered by 7 days ago
Edited 6 days ago

Very simple just use "break" inside the loop

example:

while true do
    wait()
    print("I love beans")

    break
end

but I dont have all your code to make it but hopefully you already get it. Hope I helped you LOL

0
Didn't work MattVSNNL 187 — 6d
0
you have to put the break inside the loop sorry I put it inside the if statement in my example acediamondn123 73 — 6d
Ad

Answer this question