Greetings. Another day, another problem. The goal here is to write a ServerEvent (for say an action for a character) that loops infinitely (like spinning or something), but can be toggled off. I set it up in a ways so that it syncs with a local script that will send either a 'true' or 'false' statement (hense the Toggle var in the function).

The problem here is I can get the loop to start on ServerEvent connect, however getting it to stop has proved fruitless.

script.Parent.onServerEvent:Connect(function(Player, Toggle)
	local bool = Toggle
	if bool == true then
		while wait() do
			print("This is looped!")
		end
	end
	if bool == false then
		print("No more loop!")
	end
end

This is a example script I wrote to summarize what I'm trying to achieve. Any help would be appreciated!

EDIT: I did eventually figure it out! I used a BoolValue that was stowed away inside the Character, and then had the repeat until wait for that to go false, to turn everything off. I was however put in the right direction by the fine fellow theking48989987. Thank you!

3 comments

0

because if bool==true, the loop is going to endlessly run, as wait() is never going to be false or nil

0

try using a repeat until loop instead

0

I changed the loop into a repeat until bool == false, but even after running a false statement it won't stop looping. Any other ideas?