Clickable Union to change Material and Color?

I am quite noobish to scripting and understanding what function will do what.
After multiple failure attempts to develop a script that on click of union it will change the material and color of that said union.

However, I am able to achieve it changing upon click;
Though, I wish for it to be able to change back to it’s original state of previous properties on second click.

Desired way of union property change;

Start:

  1. Material = SmoothPlastic
  2. Color = Really Black

On-Click:

  1. Material = Neon
  2. Color = Institutional White

Second Click:

  1. Material = SmoothPlastic
  2. Color = Really Black

Solutions?

I have spent a good amount of time surfing through multiple posts, but I can’t quite find what I need.

2 Likes

We aren’t here to write code for you. You said you’d made attempts to script this before. If you could post those, it would be a lot more helpful than simply stating what it is you want.

4 Likes

you could do something like this

local Switch = false 

function UnionChange() -- call this function when a player clicks the union
if Switch == false then
Switch = true
--Change the Color and Material here
elseif Switch == true then
Switch = false
--change it to original
end
2 Likes

Look into using a ClickDetector if you haven’t tried that already. That approach will probably be the easiest for you. Add the ClickDetector to your union and then add a script to the ClickDetector. Inside the script add this bit from the example code:

local ClickDetector = script.Parent
local union = ClickDetector.Parent    -- reference to your union object

ClickDetector.MouseClick:Connect(function()
    -- put your toggle code for material and color here
end)

Since you are just turning the union from black to white neon and back, a simple true/false flag should be all you need to tell it which color/material needs to be applied. Don’t forget to turn on UsePartColor for the union.

2 Likes

seems he already knows how to do a mouse click function, he just needed a way to change it back on the second click

3 Likes

Ah. I must have glossed over that. The hard part is done then. Do what Quoteory says.

2 Likes

In regards of “change the color and material here”;

Would I write it as;
“script.parent.BrickColor = BrickColor.new(248,248,248)”
"script.parent.Material = Material.new(“Neon”)
or
"Part.BrickColor = BrickColor.new(“Institutional White”)
"Part.Material = “Neon”

2 Likes

Either or for the BrickColor just make sure it’s script.Parent and not script.parent
for the material you need to do this

Part.Material = Enum.Material.Neon
1 Like

I very much thank you for helping me,

I am really new to this, and I appreciate you following along with what I have.

1 Like

Always glad to help

make sure to mark the post of the person who provides a solution to your issue so people know not to provide a solution after one has already been provided

1 Like

A couple more questions, if you don’t mind.

81c3b668304b8d75f086091e10ae143d

If I added a click-detector;
Wouldn’t I add

script.Parent.ClickDetector.MouseClick:connect(function()

I apologize, I don’t know much…

In regards of that photo,
Is that the proper way of writing that script?
I may lack a lot of “knowledge” in-order to make that “work”.

Forgot to mention; “Sign” is the name of that Union.

1 Like

Never apologize for not knowing something in programming, you will always be learning

functions with no name are called anonymous functions
e.g

script.Parent.ClickDetector.MouseClick:connect(function()

end

you can connect to other functions when events by doing

script.Parent.ClickDetector.MouseClick:connect(FUNCTIONNAMEHERE)

the final script would be something like

local Switch = false 

function UnionChange() -- call this function when a player clicks the union
if Switch == false then
Switch = true

script.Parent.BrickColor = BrickColor.new("White") -- BrickColors have names and have to be spelled correctly 
script.Parent.Material = Enum.Material.Neon

elseif Switch == true then
Switch = false

script.Parent.BrickColor = BrickColor.new("Really black")
script.Parent.Material = Enum.Material.SmoothPlastic
end

script.Parent.ClickDetector.MouseClick:connect(UnionChange)

2 Likes

I do sincerely apologize,
The script isn’t working as I would suspect it would.


Would there be add-ons such as “ServerScriptService” or anything like that?
Again, I’m not sure what things are meant for.

Don’t worry about it not working. As Quoteory said, programming is about learning.

If you want that script to work, the union must be in the workspace and have the script as it’s child. To do this in studio, copy the script and right click on the union. Then, click paste into. Check if the script is the child of the union

1 Like

Beware that if the colour of the union isn’t changing but the material is, you have to change a property in the union called UsePartColour to true

1 Like

As far as I know, you can’t really change the color of union, unless you set the value of UsePartColor property to true, like @RedDuck765 said.

Overall, @Quoteory had given a quick idea of what’s the script going to be like. However, it also have some flaws. For example, :connect is deprecated, you should use :Connect instead and forgot to put an ‘end’ for either the function/if condition.

I’ve tried the script below, and it works as long as you put the script under the union.

local switch = false 
script.Parent.UsePartColor = true

function UnionChange()
if switch == false then
	switch = true
	script.Parent.BrickColor = BrickColor.new("White") -- BrickColors have names and have to be spelled correctly 
	script.Parent.Material = Enum.Material.Neon
elseif switch == true then
	switch = false
	script.Parent.BrickColor = BrickColor.new("Really black")
	script.Parent.Material = Enum.Material.SmoothPlastic
end
end

script.Parent.ClickDetector.MouseClick:Connect(UnionChange)

You should use a Click Detector, and a ServerScript inside of it assuming you want the change to be visible for everyone.
If you want it for just the player who clicked it, there will be a lot more to cover.

We then want to detect when a player has clicked it.

So we’d start with this:

----- Items -----

local clickDetector = script.Parent

local union = clickDetector.Parent

union.UsePartColor = true

We identify the items we’re going to need first, so the ClickDetector and then the Union.

To make sure the union will change color, we have to make the property “UsePartColor” true.

Then we have a factor “clicked”, this will tell us if it was the first click or not and switch back and fourth.

----- Factors -----

local clicked = false

Then we can create a function that will change this value (true/false) everytime it’s called.
When it switches, we’re going to see what the value is (true/false) then change the union accordingly.

----- Functions -----

local function onClick()

clicked = not clicked

if clicked then

union.Material = Enum.Material.Neon

union.BrickColor = BrickColor.new("Institutional White")

else

union.Material = Enum.Material.SmoothPlastic

union.BrickColor = BrickColor.new("Really black")

end

end

Then we connect the function to the MouseClick event.

----- Code -----

clickDetector.MouseClick:connect(onClick)

EDIT:
The final code would look like this

----- Items -----

local clickDetector = script.Parent

local union = clickDetector.Parent

union.UsePartColor = true

----- Factors -----

local clicked = false

----- Functions -----

local function onClick()

clicked = not clicked

if clicked then

union.Material = Enum.Material.Neon

union.BrickColor = BrickColor.new("Institutional White")

else

union.Material = Enum.Material.SmoothPlastic

union.BrickColor = BrickColor.new("Really black")

end

end

----- Code -----

clickDetector.MouseClick:connect(onClick)

----------------------- End of Edit

I’m aware similar code was posted, but I wanted to give more insight on what the code was doing that way you’re able to understand and hopefully learn :slight_smile:

1 Like

Thank you so very much on detailing what does what and how it corresponds within the script. This was extremely helpful.

Thank you!

1 Like

Thank you as well for helping me out and describing what had gone wrong.

Thank you again! :yum:

1 Like