Why does this while wait loop get faster?

So im scripting this weapon system, and every time i fire a remote event the loop gets faster, example :

elseif TP1.Name == "UV" then
			if TP2 == "On" then
				if TP1.Battery.Value >10 then
					print("Set1")
					TP1.CurrentlyOn.Value = true
				end
			elseif TP2 == "Off" then
				print("SetOff")
				TP1.CurrentlyOn.Value = false
			end
		end
		coroutine.wrap(function()
			while task.wait(0.8) do
				if TP1.CurrentlyOn.Value == true then
					print("AE")
				end
			end
		end)()

Give me a link to that game because I want to play it on release.

1 Like

Because each time the event is fired a new coroutine is created.

elseif TP1.Name == "UV" then
	if TP2 == "On" then
		if TP1.Battery.Value >10 then
			print("Set1")
			TP1.CurrentlyOn.Value = true
		end
	elseif TP2 == "Off" then
		print("SetOff")
		TP1.CurrentlyOn.Value = false
	end
end
coroutine.wrap(function()
	while task.wait(0.8) do
		if TP1.CurrentlyOn.Value == true then
			print("AE")
		end
	end
	coroutine.yield()
end)()

You can either add a yield like I have above, or just remove the coroutine.wrap() call entirely.

even if i remove the coroutine wrap it goes fast and, coroutine.yield() has the same result

elseif TP1.Name == "UV" then
	if TP2 == "On" then
		if TP1.Battery.Value >10 then
			print("Set1")
			TP1.CurrentlyOn.Value = true
		end
	elseif TP2 == "Off" then
		print("SetOff")
		TP1.CurrentlyOn.Value = false
	end
end
coroutine.wrap(function()
	while task.wait(0.8) do
		if TP1.CurrentlyOn.Value == true then
			print("AE")
		else
			break
		end
	end
end)()

It’s possibly because there isn’t a break statement to break out of the loop.

1 Like