Script doesnt continue

I am trying to make an rpg loop and i want to check if an enemy is alive. If they are, it runs normal, else it will continue and skip it. Conditional continues in studio work really weird and some conditions in this loop might not even work

while true do
	for i,thing in ipairs(things) do
		print(thing.Name)
		if thing:FindFirstChildWhichIsA("Humanoid").Health == 0 then
			continue
		end
		if game.Players:GetPlayerFromCharacter(thing) then
			playerturn(thing)	
		else
			enemyturn(thing)
		end
	end
	task.wait()
end

is this my own error or studios?

1 Like

What is inside the things table. Are they enemy ai or players?

while true do
	for i,thing in ipairs(things) do
		print(thing.Name)
		if thing:FindFirstChildWhichIsA("Humanoid").Health == 0 then
                        print("skipped iter "..tostring(i))
			continue
		end
                print("passed iter "..tostring(i))
		if game.Players:GetPlayerFromCharacter(thing) then
			playerturn(thing)	
		else
			enemyturn(thing)
		end
	end
	task.wait(20)
end

See if when it skips on an iteration whether or not it actually does skip the rest of the code for that iteration.

I slowed down the loop so it make it easier to read.
Are things characters?

both enemies and players are inside of the things table

would the wait help if its in the outer loop

The code I sent to you is a simple debugging measure, it’s not anything fixed. I don’t see a problem with your code from what you have given us, but I want you to run it and see if the continue actually does work.

ok, also the things are both players and enemies

Replace continue with return or remove it and use a conditional block to skip the rest of the loop body. Also,ensure the Humanoid exists before checking its Health to avoid errors

while true do
	for i, thing in ipairs(things) do
		print(thing.Name)
		local humanoid = thing:FindFirstChildWhichIsA("Humanoid")
		if humanoid and humanoid.Health == 0 then
			-- Skip to next iteration
		else
			if game.Players:GetPlayerFromCharacter(thing) then
				playerturn(thing)
			else
				enemyturn(thing)
			end
		end
	end
	task.wait()
end

Hope this helps you!

UPDATE:I verified my response better,with some documentations,and i don’t really know if this would help you,let me know if that solved your problem

You didn’t check if the Humanoid exists before using it, and checking for exactly 0 health can miss cases; that’s why it breaks.

i took some code from someone else to print whether it skips or not and even when iterating over dead enemies, it says that it passed and doesn’t skip an iteration, i think it might have to do with how ipairs is implemented

1 Like

but it doesn’t throw an error when trying to get the humanoid

Hmm, probably best to switch from ipairs to a numeric for loop to avoid weird iteration issues,also, make sure to check if the Humanoid exists before checking its health to skip dead enemies properly

while true do
	for i = 1, #things do
		local thing = things[i]
		print(thing.Name)
		local humanoid = thing:FindFirstChildWhichIsA("Humanoid")
		if humanoid and humanoid.Health == 0 then
			print("Skipped: " .. thing.Name)
		else
			print("Passed: " .. thing.Name)
			if game.Players:GetPlayerFromCharacter(thing) then
				playerturn(thing)
			else
				enemyturn(thing)
			end
		end
	end
	task.wait()
end

You need to make sure the Humanoid actually exists before you check its health, and instead of continuing, just use an if condition to skip over the current loop iteration when the enemy is dead. It’s just how Lua handles these situations.

And whilst you are doing that, RunService.Heartbeat would be much better.

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	for i = 1, #things do
		local thing = things[i]
		print(thing.Name)
		local humanoid = thing:FindFirstChildWhichIsA("Humanoid")
		if humanoid and humanoid.Health == 0 then
			print("Skipped: " .. thing.Name)
		else
			print("Passed: " .. thing.Name)
			if game.Players:GetPlayerFromCharacter(thing) then
				playerturn(thing)
			else
				enemyturn(thing)
			end
		end
	end
end)

used your code just to see and the problem is still prevalent, honestly might just be studio screwing me over

Hmm, probably a good idea to use RunService.Heartbeat (as @TeamDreams123 said) instead of task.wait() for smoother looping. Also, it seems like the things table might be getting modified during iteration, or the Humanoid health isn’t updating as expected

local RunService = game:GetService("RunService")

RunService.Heartbeat:Connect(function()
	local thingsCopy = {unpack(things)} 
	for i = 1, #thingsCopy do
		local thing = thingsCopy[i]
		print(thing.Name)
		local humanoid = thing:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			print(thing.Name .. " Health: " .. humanoid.Health)
			if humanoid.Health <= 0 then
				print("Skipped: " .. thing.Name)
			else
				print("Passed: " .. thing.Name)
				if game.Players:GetPlayerFromCharacter(thing) then
					playerturn(thing)
				else
					enemyturn(thing)
				end
			end
		else
			print("No Humanoid in: " .. thing.Name) 
		end
	end
end)

unpack() is a Lua function that takes a table and returns its elements as separate values

the function also wait for some events to happen so when testing this it didnt work

If with RunService.Heartbeat didn’t work,try this:

while true do
	local thingsCopy = {unpack(things)}
	for i = 1, #thingsCopy do
		local thing = thingsCopy[i]
		print(thing.Name)
		local humanoid = thing:FindFirstChildWhichIsA("Humanoid")
		if humanoid then
			print(thing.Name .. " Health: " .. humanoid.Health) 
			if humanoid.Health <= 0 then
				print("Skipped: " .. thing.Name)
			else
				print("Passed: " .. thing.Name)
				if game.Players:GetPlayerFromCharacter(thing) then
					playerturn(thing)
				else
					enemyturn(thing)
				end
			end
		else
			print("No Humanoid in: " .. thing.Name) 
		end
	end
	task.wait()
end

oops, i just realized that i was damaging enemies by bouncing on their heads(this is based off mario and luigi) and the script was running client side, thank you ill check soon if it works

1 Like

Is things a table or a folder in the workspace?

while task.wait() do
	for _, thing in things do
		local Player = game.Players:GetPlayerFromCharacter(thing)
		local Humanoid = thing:FindFirstChildWhichIsA("Humanoid")
		
		if Humanoid and Humanoid.Health <= 0 then
			break -- Died
		elseif Humanoid and Humanoid.Health >= 0 and Player then
			playerturn(thing)
		elseif Humanoid and Humanoid.Health >= 0 and not Player then
			enemyturn(thing)
		end
	end
end