How to stop a for i,v in pairs loop after looping through all the items in the table?

I want a decal to change through all the decals in a folder and after that it returns to the regular decal.

Then as the regular decal to the end of the table and you’re done, it stops automatically

A for loop stops automatically after looping through every single item in a table, if you’re are talking about completely stopping it, use break which will stop the loop, to skip an item, use continue, it will move on to the next iteration.

1 Like

let me show you my code i think its because im using a while loop.

local man = game.Workspace.BIGMAN100000

local head = man.Head
local foodhb = man.FOODHITBOX
local defaultface = man.Head.face.Texture
local folder = game.Workspace.SmileFolder:GetChildren()

local eating = man.EAT

head.Touched:Connect(function(hit)
	if hit.Name == "Handle" then
		hit.Parent:Destroy()
		print("MMMMYUMMY!!!!")
		eating = true
		while eating do
			for i,v in pairs(folder) do
				head.face.Texture = v.Texture
				head.FOOOD.Playing = true
				wait()
				head.face.Texture = defaultface
				head.FOOOD.Playing = false
			end
		end
	end
end)

where do i put the eating = false to end the decals looping?

If the condition inside the while loop is no longer met, meaning that eating is false, it will automatically break, you may have to put inside your loop as it wont anything after until it breaks, you should also handle this outside the Touched event.

yea but if i put it into the for i,v loop it will loop through only a single decal

any ideas what i should use to trigger the value change?

Simply use .Changed

Changed

You should be using Changed to check if a value changed.

it doesn’t seem to work though? i think its because the function only checks the value on touch how can i fix this?


local eating = man.EAT

head.Touched:Connect(function(hit)
	if hit.Parent.Name == "TOOL" then
		if eating.Value == false then
			hit.Parent:Destroy()
			print("eating")
			eating.Value = true
			while eating do
				for i,v in pairs(folder) do
					head.face.Texture = v.Texture
					head.FOOOD.Playing = true
					wait(0.1)
			    end	
			end
		end
	end
end)

eating.Changed:Connect(function()
	if eating.Value == true then
		wait(1.5)
		eating.Value = false
		head.face.Texture = defaultface
		head.FOOOD.Playing = true
	end
end)

you can break the for loop.

1 Like

anyone there? i still haven’t solved it