While true do breaking

Is There A way to break while true do loops whenever The Player Dies Or Resets

just use return end in the while loop to break it.

While-loops have conditions. A lot of people seem to forget that since they are often convinced early on that “while true do” is the only way to make a loop.

In nearly every case something like this:

while (true) do
    if (exit_condition) then
        break
    end
    ...
end

Can always be replaced with something like this:

while (looping_condition) do
    ...
end

Where looping_condition is the inverse of exit_condition

May I asked what the loops is for you may not need one.

--sorry if this doesnt work right away I didn't test it
game.Players.PlayerAdded:Connect(function(Player)
  Player.CharacterAdded:Connect(function(Character)
    local LoopBool = true
    local Humanoid =  Character:WaitForChild("Humanoid")
    Humanoid.Died:Connect(function()
      LoopBool = false
    end)
    while LoopBool  do
      wait()
    end
  end)
end)

Coroutines.

@LuaDeveloped I Have a part in the game that Turns Transparent every 7 seconds but I want to break the script when the player dies

Well you can use this part to achieve destroying the script when a player dies. Also do you want this to be done locally, or on the server. This script is made to be a server script and when any players die it would destroy it.

game.Players.PlayerAdded:Connect(function(Player) --when a player joins the game this will fire
  Player.CharacterAdded:Connect(function(Character) --when the players character is created this will fire
    local Humanoid = Character:WaitForChild("Humanoid") --wait until the character has a humanoid
    Humanoid.Died:Connect(function() --when the humanoid is died this will fire
      --Check if Script is nil
      --Destroy Script Here
    end)
  end)
end)
2 Likes