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

What is wrong with this GUI? (Solved)

On=false 
function onClicked() 
if On == false then
On = true 
while On == true do

    script.Parent.Image = "http://www.roblox.com/asset/?id=173364351"
    wait(0) 

while On == false do
	
	script.Parent.Image = "http://www.roblox.com/asset/?id=173364314"
    wait(0)
end  
end
end
end


script.Parent.MouseButton1Down:connect(onClicked)

I am starting to lose my thoughts, anybody want to help? I may seem like a fool asking this. So far the turning on works but the turning off doesn't work.

2 answers

✓ Accepted answer

There is a much simpler way to organize this.

function onClicked()
	On = not On;
	-- switches `On` between `true` and `false`
end

script.Parent.MouseButton1Down:connect(onClicked)

while wait(0) do
	local tex = "rbxassetid://173364314";
	if On then
		tex = "rbxassetid://173364351";
	end
	script.Parent.Image = tex;
end

This avoids the loops inside of the OnClicked and makes it much, much clearer.


At this point, though, we'll see that the loop itself is redundant. It only changes whenever On changes, so we can just move the if into the onClicked function:

function onClicked()
	On = not On;
	local tex = "rbxassetid://173364314";
	if On then
		tex = "rbxassetid://173364351";
	end
	script.Parent.Image = tex;
end

script.Parent.MouseButton1Down:connect(onClicked)

And now we have a much, much simpler, cleaner solution. (The first snippet could be used if there was some sort of animation that was dependant on whether or not it was on; the second one lacks this functionality)

On=false 
function onClicked() 
	if On == false then
		On = true 
		while On == true do
			script.Parent.Image = "http://www.roblox.com/asset/?id=173364351"
			wait(0) 
			on = false -- You forgot to set on as false.
		while On == false do -- You needed on to be false to allow this to work.
 			script.Parent.Image = "http://www.roblox.com/asset/?id=173364314"
			wait(0)
		end  
		end
	end
end


script.Parent.MouseButton1Down:connect(onClicked)

You forgot to set on as false after the first part. I'm not sure if this may also affect it, but maybe you should use an if/else script since it seems that the button may break when clicked though I'm not sure about it. Just try this script for now and if there are any problems tell me. :D

2 comments

2

If you're going to answer a question and respost their code, you should correct the tabbing as a good example.

0

Yeah, I guess I should edit that. Thanks for the suggestion.

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