Loop isn't working

So I’m a beginner scripter with about six months of experience by now, and I decided to write my very first code all by myself without a tutorial. Hurray!

So I decided that when a person touches this alarm brick thing, the lights flash red and white. When that specific person dies, then everything goes back to normal.

So I test it and everything is working except that the lights aren’t flashing. They flash once and then they stop. Here’s that bit:

repeat
 wait(0.5)
 securitylite.Color = Color3.new(170,0,0)
 securityliteB.Color = Color3.new(170,0,0)
 wait(0.5)
 securitylite.Color = Color3.new(255,255,255)
 securityliteB.Color = Color3.new(255,255,255)
until player.Humanoid.Died

The variables are working for sure. This is in a bigger function, and everything else works. I know that this is the only bit that doesn’t work.

Thank you for anyone who replies and, no, there is nothing in the output.

2 Likes

Well I would use “for” or “while true do” for loops, I used “for” in my first script alone

While true do
script
end

“Any name” " how much times u want to repeat"
for I = 1, 100 do

script

end

1 Like

I want the lights to keep flashing until the person dies. I don’t know if I made that clear.

I did try “while not do” and added a wait (0.5) at the end, the lights didn’t even flash once.

@NoodleCrystalHaden10, Perhaps this could work to replace that bit of code (repeat until can be iffy):

while player.Character.Humanoid.Died == false do
    wait(0.5)
    securitylite.Color = Color3.new(170,0,0)
    securityliteB.Color = Color3.new(170,0,0)
    wait(0.5)
    securitylite.Color = Color3.new(255,255,255)
    securityliteB.Color = Color3.new(255,255,255)
end

Also, I noticed you did player.Humanoid.Died, even though Humanoid is inside of the player’s Character. Assuming your player variable is the player object, you’ll want to do player.Character.Humanoid, not player.Humanoid.

The “player” variable is actually the physical part. The one in Workspace, not Players (is it PlayerService?) So the format would go; game.Workspace. (here is the player). Humanoid (there’s no character before it)

I’m sorry, I tried it, didn’t flash once.

Ah okay.

Could you try placing two print()'s in your code? One right before the loop, and one at the beginning of it. Like this:

print("First")
while player.Character.Humanoid.Died == false do
    print("Second")
    wait(0.5)
    securitylite.Color = Color3.new(170,0,0)
    securityliteB.Color = Color3.new(170,0,0)
    wait(0.5)
    securitylite.Color = Color3.new(255,255,255)
    securityliteB.Color = Color3.new(255,255,255)
end

Let which one(s) print.

Okay, I’ll try it. I’ll let you know.

“first” prints, but “second” doesn’t print.

I just realized what the problem is. Humanoid.Died is an event, not something that returns a boolean value. Thus, if you want to check if the player is still alive, you can do it directly by checking their health. Here’s the new code:

print("First")
while player.Character.Humanoid.Health > 0 do
    print("Second")
    wait(0.5)
    securitylite.Color = Color3.new(170,0,0)
    securityliteB.Color = Color3.new(170,0,0)
    wait(0.5)
    securitylite.Color = Color3.new(255,255,255)
    securityliteB.Color = Color3.new(255,255,255)
end

Congratulations on getting the solution! I just needed to take the character bit out making it player.Humanoid.Health but thank you!!!

1 Like

Just make a for loop. They are a lot less glitchy.