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

Toggle script (true/false switch) is incorrect, help please ?

I'm trying to do a test for later usage, it is about toggles. This is what I plan to do: When I click on a brick, it toggles between two different colors.

Here it is:

Trigger = script.Parent:WaitForChild('Switch').Trigger
Toggle = false

Trigger.MouseButton1Click:connect(function()
	if Toggle == false then
		Trigger.BrickColor = BrickColor.Red()
			Toggle = true
	else
		Trigger.BrickColor = BrickColor.Green()
			Toggle = false
	end
end)

It seems not to work... I have tried a lot of versions, which none of them worked, could anyone help me find the real, the functioning one, please ?

3 answers

Here, let's try the following:

Trigger = script.Parent:WaitForChild('Switch').Trigger
Toggle = false

Trigger.MouseButton1Click:connect(function()
    if Toggle == false then
	--Your problem is here. It's not BrickColor.Red(), it's BrickColor.new("Red"). But Red doesn't exist, so what you would want is BrickColor.new("Really red"): A red variant, pretty similar to the original one.
        Trigger.BrickColor = BrickColor.new("Really red")
            Toggle = true
    else
	--Your problem is ALSO here. It's not BrickColor.Green(), it's BrickColor.new("Green"). But, once more, Green doesn't exist, so what you would want is BrickColor.new("Dark green"): A green variant, pretty similar to the original one.
        Trigger.BrickColor = BrickColor.new("Dark green")
            Toggle = false
    end
end)

If this works, please mark as the solution!

As always, good scripting!

5 comments

0

You're only fixing one issue with his script, which is BrickColor.new(), the other issue is because he isn't checking if Toggle is equal to true.

0

I didn't even notice it before !

0

@supersmartnoobguy Can you mark this as the answer? It took some time to write, besides, it's the correct one!

0

I could, but it didn't work

0

You should not really be asking for him to mark it as the answer.

Hope this resolves your issue.

Trigger = script.Parent:WaitForChild('Switch').Trigger
Toggle = false

Trigger.MouseButton1Click:connect(function(OnClicked)
    if Toggle == false then
        Trigger.BrickColor = BrickColor.new("Really red")
            Toggle = true

    elseif Toggle == true then
        Trigger.BrickColor = BrickColor.new("Bright green")
            Toggle = false
    end
end)

I managed to find and fix it now, I appreciate that you guys wanted to help me, but none of the answers you gave me were correct...

Boy = script.Parent
Toggle = true

function OnClicked()
	if Toggle == true then
		Boy.BrickColor = BrickColor.new("Bright green")
		Toggle = false
	else
		if Toggle == false then
			Boy.BrickColor = BrickColor.new("Bright red")
			Toggle = true
		end
	end
end

Boy.ClickDetector.MouseClick:connect(OnClicked)

2 comments

0

Did you try my version? It is alot cleaner. And now you have switched it to a ClickDectector? You could've of elaborated your question more to tell us that it was for a ClickDetector and not a GraphicalUserInterface. Completely your fault, and what a waste of time. Real, nice.

0

Sorry, my bad !

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