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

help, how can i changed a tool to red and blue every time it's clicked?

i'm trying to make a tool that changes it's color every time it's clicked, i tried to do it with a while loop and with 2 events but it didn't solicited into something i expected

code sample:

local tool = script.Parent

while true do
	
	tool.Activated:Connect(function()
		tool.Handle.BrickColor = BrickColor.new("Bright blue")
	end)
	
	wait()
	
	tool.Activated:Connect(function()
		tool.Handle.BrickColor = BrickColor.new("Bright red")
	end)
	
end

2 answers

✓ Accepted answer

There are some slight mistakes in your code. Let's go through it together! In the third line, while loop should not be used with RBXScriptSignals. After you delete the loop, you'll see that you have two exactly the same signals, which is not good if you don't want to waste some memories of signals. Let's delete the first signal and declare a boolean value called:clicked. Now your code should look like this:

local tool = script.Parent

local clicked = false
tool.Activated:Connect(function()
	clicked = not clicked
	tool.Handle.BrickColor = clicked and BrickColor.new("Bright blue") or BrickColor.new("Bright red")
end)

Great! It worked perfectly with some code sugars and and or, which is exactly as same as this code block:

local tool = script.Parent

local clicked = false
tool.Activated:Connect(function()
	clicked = not clicked
	if clicked then
		tool.Handle.BrickColor = BrickColor.new("Bright blue")
	else
		tool.Handle.BrickColor = BrickColor.new("Bright red")
	end
end)

After this, if you want to look stylish and cool then:

local tool = script.Parent

local clicked = false

local eqp = tool.Equipped:Connect(function()
	local act = tool.Activated:Connect(function()
		clicked = not clicked
		tool.Handle.BrickColor = clicked and BrickColor.new("Bright blue") or BrickColor.new("Bright red")
	end)
	local ueq
	ueq = tool.Unequipped:Connect(function()
		act:Disconnect()
		ueq:Disconnect()
	end)
end)

NOTE: This only works with 2 states. I recommend using enumeration-like methods with statements.

3 comments

0

oh right, this one is better

0

Thx!

0

your code works, thx

While loop? what's that for...

Your script probably stopped because while true do loop without any slow downs will cause script exhausting. You don't need it there as it is only enough to connect the function once and it will fire every time the tool is activated. So:

local tool = script.Parent
	 
tool.Activated:Connect(function()
	tool.Handle.BrickColor = BrickColor.new("Bright blue")
end)

Right but... red and blue every time it's clicked? Well, for that all you need is a variable to check if the tool color is red or blue.:

local tool = script.Parent
local currentColor = BrickColor.new("Bright Blue")
	 
tool.Activated:Connect(function()
	tool.Handle.BrickColor = currentColor
end)

Now the current color is made but here is how you change it to red and blue and red and...

local tool = script.Parent

local blue = BrickColor.new("Bright Blue")
local red = BrickColor.new("Bright Red")

local currentColor = blue

tool.Activated:Connect(function()
	currentColor = (currentColor == blue and red) or blue
	tool.Handle.BrickColor = currentColor
end)

You should modifty it a bit as i am not currently in studio so the code may not be the best. Here is how line 9 works:

currentColor = (currentColor == blue and red) or blue

So first it checks if the current is Blue and if it is blue then by using and statement it switches to next value which is red, but if it is NOT blue then it uses the or statement to switch the color to blue.

About 'AND' and 'OR'

1 comment

0

oops, i accepted the wrong answer and btw your code doesnt work :(

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