Hey guys, I am working on a FNAF-like game and I am trying to make it so the monsters reset after your character dies. (Ignore the name of the monster and model those are just placeholders) I currently am using a while loop which loops until 6 am is reached (if u know fnaf) and when my character dies I just set a value to 6 as the while loop runs while the value is < 6.
The annoying thing is that after the monster is reset, the loop runs again, making it possible for my monster to move. (I make it move every 4 seconds with 100% possibility for testing purposes) I also print out every 4 seconds and after i reset, it prints, even though I think ive exited out of the loop already. I’ve tried making my code simpler, making it neater kinda (im not very neat) and changing how and where the break statement runs.
This is my code:
local resetted = false
rstorage.revents.night1.OnServerEvent:Connect(function()
local seconds = 0
rstorage.timer.Value = 0
resetted = false
rstorage.revents.death.OnServerEvent:Connect(function()
rstorage.timer.Value = 6
seconds = 0
task.wait(3)
reset()
resetted = true
end)
while rstorage.timer.Value < 6 do -- runs until 6 am
--print("loop activated")
if resetted == true then
break
end
task.wait(4)
local hit = 1 --Math.Random(1,3) -- 1 in 3 chance to move hence 1,3
if hit == 1 then
moveBigChig()
else
print("did not move")
end
seconds += 4
--update every minute
if seconds == 60 then
rstorage.timer.Value += 1
seconds = 0
end
print(seconds)
end
end)
(I didn’t put the entire script since this is a server script but rstorage is replicated storage u probably knew that and the move function doesn’t affect the loop)
i appreciate any help as this is my first post lol