Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

What is wrong with this GUI? (Solved)

Asked by 10 years ago

Please make your question title relevant to your question content. It should be a one-sentence summary in question form.
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

Log in to vote
1
Answered by
BlueTaslem 18071 Moderation Voter Administrator Community Moderator Super Administrator
10 years ago

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)

Ad
Log in to vote
0
Answered by
Nifer2 174
10 years ago
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
If you're going to answer a question and respost their code, you should correct the tabbing as a good example. BlueTaslem 18071 — 10y
0
Yeah, I guess I should edit that. Thanks for the suggestion. Nifer2 174 — 10y

Answer this question