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

My while true do loop will not run at all, why is this so?

Asked by 4 years ago
Edited 4 years ago

So I am trying to make an elevator game. But the main code will not run, why is this so? There's no errors

My code is





local plrs = {} game.Workspace:WaitForChild("TpCframe1").Touched:Connect(function(hit) if hit.Parent:FindFirstChild("Humanoid") and db == false then db = true hit.Parent.HumanoidRootPart.CFrame = game.Workspace.TpCframe2.CFrame local player = game.Players:GetPlayerFromCharacter(hit) if not table.find(plrs,player) then -- detecting if player is not in the table table.insert(plrs,player) -- adding player in table print("Added player to table.") wait(1) db = false end end end) while true do wait(0.01) if #plrs >= 1 then wait(1) for i = 10,0,-1 do wait(1) game.ReplicatedStorage.Timer.Value = i if i == 0 then local event = game.ServerStorage.Floors:GetChildren() local chosenEvent = event[math.random(1,#event)] local clonedEvent = chosenEvent:Clone() clonedEvent.Parent = workspace wait(2) open() wait(0.5) if chosenEvent.Name == "No Chill" then clonedEvent.Board.chill.Face = Enum.NormalId.Front wait(1) clonedEvent.Board.chill.Face = Enum.NormalId.Top clonedEvent.Board.wrong.Face = Enum.NormalId.Front game.Workspace["WRONG!"]:Play() wait(1) clonedEvent.Board.checkit.Face = Enum.NormalId.Front clonedEvent.Board.wrong.Face = Enum.NormalId.Top wait(1) clonedEvent.Board.checkit.Face = Enum.NormalId.Top clonedEvent.Board.correct.Face = Enum.NormalId.Front game.Workspace.correct:Play() wait(1) end for i,v in pairs(game.Players:GetPlayers()) do if v then v.Character:WaitForChild("Humanoid").Died:Connect(function() table.remove(plrs,v) end) end end game.Players.PlayerRemoving:Connect(function(plr) table.remove(plrs,plr) end) wait(15) clonedEvent:Destroy() break end end end wait() end

2 answers

Log in to vote
3
Answered by 4 years ago

Try adding a wait like this:

while true do
    if #plrs >= 1 then
        wait(1)
        for i = 10,0,-1 do
            wait(1)
            game.ReplicatedStorage.Timer.Value = i
            if i == 0 then
                local event = game.ServerStorage.Floors:GetChildren()
                local chosenEvent = event[math.random(1,#event)]
                local clonedEvent = chosenEvent:Clone()
                clonedEvent.Parent = workspace
                wait(2)
                open()
                wait(0.5)
                if chosenEvent.Name == "No Chill" then
                    clonedEvent.Board.chill.Face = Enum.NormalId.Front
                    wait(1)
                    clonedEvent.Board.chill.Face = Enum.NormalId.Top
                    clonedEvent.Board.wrong.Face = Enum.NormalId.Front
                    game.Workspace["WRONG!"]:Play()
                    wait(1)
                    clonedEvent.Board.checkit.Face = Enum.NormalId.Front
                    clonedEvent.Board.wrong.Face = Enum.NormalId.Top
                    wait(1)
                    clonedEvent.Board.checkit.Face = Enum.NormalId.Top
                    clonedEvent.Board.correct.Face = Enum.NormalId.Front
                    game.Workspace.correct:Play()
                    wait(1)
                end
                wait(15)
                clonedEvent:Destroy()
            end
        end
    end
    wait()
end

If this doesn't work, just let me know.

0
this would work, upvoted raid6n 2196 — 4y
Ad
Log in to vote
1
Answered by 4 years ago

I don't necessarily see any issues but here's my rewrite because I'm not going to just read this and leave you hanging.

local Workspace = (workspace ~= nil and workspace) or game:GetService("Workspace")
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RunService = game:GetService("RunService")
local ServerStorage = game:GetService("ServerStorage")
local CoordinateFrameMarker = Workspace:FindFirstChild("TpCframe1")
local PlayerList = {}

local function swait(num)
    for i = 1,(num ~= nil and num) or 1,1 do
        return RunService.Stepped:Wait()
    end
end

repeat
    swait()
until CoordinateFrameMarker ~= nil 

CoordinateFrameMarker.Touched:Connect(function(Hit)
    local Character = (Hit.Parent:IsA("Model") and Hit.Parent) or Hit:FindFirstAncestorOfClass("Model")
    if not Character then
        return
    end
    pcall(function()
        local Humanoid = Character:FindFirstChildOfClass("Humanoid")
        local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart") or Humanoid.RootPart
        local Player = Players:GetPlayerFromCharacter(Character)
        if not table.find(PlayerList,Player) then
            table.insert(Players,Player)
            print("Player added to playerlist table.");
            wait(1);
            db = false
        end
    end)
end)

coroutine.resume(coroutine.create(function()
    while true do
        swait()
        if #PlayerList >= 1 then
            wait(1)
            for i = 10,0,-1 do
                wait(1)
                ReplicatedStorage.Timer.Value = i
                local RandomEvent = ServerStorage.Floor:GetChildren()[math.random(1,#ServerStorage.Floor:GetChildren())];
                local ClonedEvent = RandomEvent:Clone()
                ClonedEvent.Parent = Workspace
                wait(2)
                open()
                wait(.5)
                if string.lower(ClonedEvent.Name) == ("no chill") then
                    ClonedEvent.Board.chill.Face = Enum.NormalId.Front
                    wait(1)
                    ClonedEvent.Board.chill.Face = Enum.NormalId.Top
                    ClonedEvent.Board.wrong.Face = Enum.NormalId.Front
                    Workspace["WRONG!"]:Play()
                    wait(1)
                    ClonedEvent.Board.checkit.Face = Enum.NormalId.Front
                    ClonedEvent.Board.wrnog.Face = Enum.NormalId.Top
                    wait(1)
                    ClonedEvent.Board.checkit.Face = Enum.NormalId.Top
                    ClonedEvent.Board.correct.Face = Enum.NormalId.Front
                    Workspace.correct:Play()
                    wait(1)
                end
                for i,v in pairs(Players:GetPlayers()) do
                    pcall(function()
                        coroutine.resume(coroutine.create(function()
                            repeat
                                wait()
                            until v.Character:FindFirstChildOfClass("Humanoid")
                            local Humanoid = v.Character:FindFirstChildOfClass("Humanoid")
                            Humanoid.Died:Connect(function()
                                pcall(function()
                                    table.remove(PlayerList,v)
                                end)
                            end)
                        end))
                    end)
                end
                Players.PlayerRemoving:Connect(function(Player)
                    pcall(function()
                        table.remove(PlayerList,Player)
                    end)
                end)
                wait(15)
                ClonedEvent:Destroy()
                break
            end
        end
        wait()
    end
end))

Answer this question