How to make this into a loop that doesn't crash/break the game?

I’m looking for a way to make this code:

for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							am:FireServer(v)
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							damageevent1:FireServer(v, hasmark, sprintdebounce)
						end
					end

Into a consistent loop (optional, could be 0.1 second breaks) without breaking the game. Also, I would like the loop to stop after a few seconds if the if statement does not activate. I’ve tried "while true do"s, but given my level of scripting I may just be using them wrong.

Please help!

something like this???

while task.wait(1) do
       for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							am:FireServer(v)
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							damageevent1:FireServer(v, hasmark, sprintdebounce)
						end
					end
end

Ok, but the loop does not stop.

ok then you can stop the loop like this

while task.wait(1) do
       for i, v in pairs(workspace:GetPartsInPart(x)) do
						if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
							am:FireServer(v)
							local x = Instance.new("IntValue", v.Parent)
							x.Name = "Hit"..Player.Name
							game.Debris:AddItem(x, 1)
							damageevent1:FireServer(v, hasmark, sprintdebounce)
						end
					end
        break --stops the loop
end

Hey, this is still the solution, but I basically want the if statement to break the loop the INSTANT it is activated. I already tried putting a break like this:

while task.wait(1) do
				for i, v in pairs(workspace:GetPartsInPart(x)) do
					if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
						
						local x = Instance.new("IntValue", v.Parent)
						x.Name = "Hit"..Player.Name
						game.Debris:AddItem(x, 1)
						dashevent:FireServer(v)
						break
					end
				end
				break --stops the loop
			end

But it didn’t work. Any thoughts?

that loop should break only if in pairs loop got broken

no it doesnt break instantly
it breaks after for i,v in pairs got broken

so how do i do that (i have pea brain)

so lets take a look at for i,v in pairs loop

for i, v in pairs(workspace:GetPartsInPart(x)) do
					if v.Parent:FindFirstChild("Humanoid") and v.Parent ~= Character and v.Parent:FindFirstChild("Hit"..Player.Name) == nil then
						
						local x = Instance.new("IntValue", v.Parent)
						x.Name = "Hit"..Player.Name
						game.Debris:AddItem(x, 1)
						dashevent:FireServer(v)
						break
					end
				end

if that “if” statement fullfils the requirements that for i,v in pairs loop will break

then after that happens while task.wait(1) do loop will also break because it waits until for i,v in pairs finishes

no but the loop takes one full second to actually AFFECT v. I want it so that when something is sensed, the Id statement activates. I’m assuming the “while task.wait(1) do” needs to go, because I don’t want the affects of the loop to come a second after it starts.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.