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

How to end a While True Do loop with a Touched function?

Asked by 10 years ago

Hello! I am making a mainscript for my game I am making called Maze Runner, and it is going great! However, I cannot find a correct method of how to end the 'minigame' correctly. I was wandering if the community could help! here is my code:


minigames = game.Lighting.Mazes:GetChildren()
h = Instance.new("Hint", game.Workspace)
so = Instance.new("Sound", game.Workspace)

 so.SoundId = "http://www.roblox.com/asset/?id=142731047"
 so:Play()
 print("MazeRunnerMain Script has loaded and the games are about to begin")
 while true do
    if game.Players.NumPlayers > 1 then
        for i = 60,1,-1 do
        h.Text = "You have " ..i.. " Seconds left to prepare."
        wait(1)
        end
        h.Text = "Choosing a Maze..."
        wait(3)
        h.Text = "Almost there..."
        wait(2) 
        h.Text = "Ah! Found one!"
        wait(2)
        rangame = math.random(1, #minigames)
        gameChosen = minigames[rangame]
        h.Text = "Loading map..."
        wait(2)
        gameChosenClone = gameChosen:Clone()
        wait(2)
        h.Text = "Done!"
        gameChosenClone.Parent = game.Workspace
        wait(3)
        so:Stop()
        wait()
        so.SoundId = "http://www.roblox.com/asset/?id=150508897"
        wait()
        so:Play()
        h.Text = "Make it to the end of the maze for the Riches of a lifetime, if you dare..."
        --how to end game on Touched?
        h.Text = "Game Over!"
        gameChosenClone:Destroy()
        wait(5)
        so:Stop()
        wait()
        so.SoundId = "http://www.roblox.com/asset/?id=142731047"
        wait()
        so:Play()
    else
        h.Text = "You need another Adventurer in order to Enter The Maze!"
    end
    wait() 
end

My issue is near the comment that says "--How to end on Touch?" Please help soon and Thanks!

1 answer

Log in to vote
1
Answered by 10 years ago

Try adding a variable called Ended and make it false. Insert this code:

--Put this at the top:
Ended = false

--at the spot:
part.Touched:connect(function(hit)
    if hit.Parent:FindFirstChild("Humanoid") then
        Ended = true
    end
end)
repeat
    if Ended == true then --Prevent the endless loop glitch
        break
    end
    wait(0.1)
until Ended == true
0
once I step on the part, the game loop constantly ends the game instead of letting the brick be stepped on at all. Please Help! FOXmcloud021 5 — 10y
0
I REALLY NEED AN ANSWER! FOXmcloud021 5 — 10y
Ad

Answer this question