Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I can't get my toggle button to work and there's no output errors?

I'm making a script that you press and it toggles on or off, when it's on it fires a server over and over again fast, there's no errors in the output so I'm assuming I'm missing something... If you need more context just ask and I can probably give.

local isOff = true
local autoSpawn = false


function leftclick()
	if isOff == true then
		script.Parent.Parent.BackgroundColor3 = Color3.new(155,155,155)
	isOff = false
	autoSpawn = true

	else 
		
	if isOff == false then
		script.Parent.Parent.BackgroundColor3 = Color3.new(255,255,255)
	autoSpawn = false
	end 
 end
end
	
while true do
	wait()
	if autoSpawn == true then
		game.ReplicatedStorage.GiveStand:FireServer()
	end	
end

script.Parent.MouseButton1Click:Connect(leftclick)

1 comment

0

its glaringly obvious why it wont work: you have an infinite loop preventing it from doing anything.

1 answer

✓ Accepted answer

Since your infinite loop is preventing the rest of the script from running and the fact that you didn't even need it in the first place, the only real solution would be to get rid of it completely.

local isOff = true
local autoSpawn = false


function leftclick()
	if isOff then
		script.Parent.Parent.BackgroundColor3 = Color3.new(155,155,155)
		isOff = false
		autoSpawn = true
		game.ReplicatedStorage.GiveStand:FireServer() --why not just fire server when you know you should rather than create an infinite loop
	else
		script.Parent.Parent.BackgroundColor3 = Color3.new(255,255,255)
		autoSpawn = false
	end 
end --you had too many ends, you would be able to see it if you hadn't deleted the default indentation 

script.Parent.MouseButton1Click:Connect(leftclick)

Since this answers your question, mark it as the accepted answer.

7 comments

0

Thanks!

0

Or you could use a coroutine.

0

After trying it I realize that it only fired the event once rather then on repeat until toggled off again but it's the best I'm going to get so I guess I'll set as answer.

0

I wouldn't recommend using coroutines to an absolute beginner. Better that they get a more in-depth understanding of how everything works before that point.

0

Actually you should not fire server in a loop like that if you aren't sending any values to the server, just have a loop server side.

0

Appreciate it mate.

0

Okay. FireServer can also cause lag and possible memory spikes.

This discussion is preserved as it was. New answers, comments, and votes are closed.