local function gameLoop()
print("loop")
while true do
boss.Health.current:GetPropertyChangedSignal("Value"):Connect(function()
if boss.Health.current.Value == 0 then
break
end
end)
if waitWithCheck(5) then break end
voidRun()
if waitWithCheck(5) then break end
cry()
if waitWithCheck(5) then break end
throwTrash()
end
hum.Health = 0
end
why does the break not work in the event?? Workspace.handler:88: break statement must be inside a loop - error
is there any alternative for this other than adding an if statment at the top of the loop. if i do that then the boss has to finish his attack loop before actually dying. thanks for replies!
The break doesn’t work because it is technically inside the function, which runs independently of the loop (for example, the function only runs when called upon instead of being ran by the loop).
If you want to stop the loop when the boss’ health reaches zero, try changing a boolean (true/false) variable instead of using the break command. Then, only run the loop when the variable is true or false, whichever indicates that the boss’ health is not zero.
Okay, then you should make a global variable that changes when the boss’ health reaches zero. Then, check that variable inside the attack functions to stop any progression if the boss is dead.
Ummm…, my advice would be not to do this inside the loop, what are you especially doing is calling the Connect function over and over in a loop which will register the event a heck ton of times, leading to a memory leak.
use this code instead:
local function gameLoop()
print("loop")
local checkVar = false
boss.Health.current:GetPropertyChangedSignal("Value"):Connect(function()
if boss.Health.current.Value == 0 then
checkVar = true
end
end)
while true do
if(checkVar) then break end
if waitWithCheck(5) then break end
voidRun()
if waitWithCheck(5) then break end
cry()
if waitWithCheck(5) then break end
throwTrash()
end
hum.Health = 0
end
Like you said, breaking the loop won’t stop the 20 second attacks. Unfortunately, if you want the ability to stop every single action the boss does as soon as it dies, that will take more than a single line of code.
i tried doing something like that, the problem is the attack loop could take up to a minute to go again, so it will only be check the health every minute.
What @ZurichBT is trying to say is making a variable and then setting it as the condition for the loop
local loop = true
while loop do
boss.Health.current:GetPropertyChangedSignal("Value"):Connect(function()
if boss.Health.current.Value == 0 then
loop = false
end
end)
if not loop then break end
--rest of code
end
the condition is only checked at the start for every loop, so if i do that. it will check if the boss is dead right away, if it isnt, it continues with the loop. if the boss dies halfway through the loop, it wont get detected until the loop goes again.
Exactly. If you want to check more often (for example, between each attack sequence), then you will have to put in a line of code for each check. There is no way around that.
Create a coroutine for the loop and then separate the event that will stop the coroutine.
local loop = coroutine.create(function()
while true do
--your code
end
end)
coroutine.resume(loop) --this should not yield, then continue to the event
local detect = boss.Health.current:GetPropertyChangedSignal("Value"):Connect(function()
if boss.Health.current.Value == 0 then
coroutine.close(loop)
detect:Disconnect()
end
end)
I just came here to say that i figured it out! I did it a bit differently, but I’ll give you the answer
here’s mine though:
-- behaviour loop
local taskCoro
local function gameLoop()
print("loop")
while true do
task.wait(5)
voidRun()
task.wait(5)
cry()
task.wait(5)
throwTrash()
end
end
-- death yield
taskCoro = coroutine.create(gameLoop)
boss.Health.current:GetPropertyChangedSignal("Value"):Connect(function()
coroutine.close(taskCoro)
print("deadddd")
hum.Health = 0
end)
-- on event`
repeat task.wait() until script.start.Value == true
print("recieved")
coroutine.resume(taskCoro)
-- on event`
repeat task.wait() until script.start.Value == true
local function gameLoop()
print("loop")
local thread = task.spawn(function()
while true do
if waitWithCheck(5) then break end
voidRun()
if waitWithCheck(5) then break end
cry()
if waitWithCheck(5) then break end
throwTrash()
end
hum.Health = 0
end)
boss.Health.current:GetPropertyChangedSignal("Value"):Connect(function()
if boss.Health.current.Value == 0 then
hum.Health = 0
task.cancel(thread)
end
end)
end