Clickable block or part to change the material?

You can write your topic however you want, but you need to answer these questions:

  1. **What do you want to achieve?

Make a button that when its pressed it changes various parts to neon then when click again changes to smooth plastic.

It would be like this:

image

(though im trying to learn the basics of scripting but it’s hard but i’ll keep trying.)

2 Likes

You can create a ClickDetector and parent it to the object you want. And detect when it’s pressed, with a server script.
example:

local cd = script.Parent -- the click detector
local brick = cd.Parent -- the object

cd.MouseClick:Connect(function(player) -- the player who pressed
   -- code here
end)
3 Likes

Here’s the developer.roblox.com link to making a click to touch Part that changes colour. You can rewrite the script to whatever you’d like to change.

2 Likes

something like this? image

1 Like

yes, but when you’re changing material you should use Enums

script.Parent.Material = Enum.Material.Neon

Edit: Also, looking at the context of the script, something like this would be better

local cd = script.Parent
local brick = cd.Parent 
local isNeon = false -- the boolean value to see if the object is neon

cd.MouseClick:Connect(function(player) -- the player who pressed
   if not isNeon then -- checks if the value of "isNeon" is false
      isNeon = true -- change the value of "isNeon" to true
      brick.Material = Enum.Material.Neon -- change the material of the brick to Neon
   else -- otherwise
      isNeon = false -- change the value of "isNeon" to false
      brick.Material = Enum.Material.SmoothPlastic -- change the material of the brick to SmoothPlastic
   end
end)
2 Likes

These links might help,

The ones below this text have a script example for what you’re trying to do

If you need a script example here one is (Disclaimer, this example is from the developer site, not my own code)

 
-- Create a ClickDetector so we can tell when the part is clicked 
local cd = Instance.new("ClickDetector", part)
 
-- This function updates how the part looks based on its Anchored state
local function updateVisuals()
	if part.Anchored then
		-- When the part is anchored...
		part.BrickColor = BrickColor.new("Bright red")
		part.Material = Enum.Material.DiamondPlate
	else
		-- When the part is unanchored...
		part.BrickColor = BrickColor.new("Bright yellow")
		part.Material = Enum.Material.Wood
	end
end
 
local function onToggle()
	-- Toggle the anchored property
	part.Anchored = not part.Anchored
	
	-- Update visual state of the brick
	updateVisuals()
end
 
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(onToggle)
2 Likes

oof it doesnt works anything else i could do?

do you get any errors in the output log? or try to use @RobloxTitan4223’s solution, or try using my edit

1 Like

nope nothing at the output oof

2 Likes

Try this, but you need to change the variable part to the location of your part

-- Create a ClickDetector and tell the script where the part is so we can tell when the part is clicked 
local part = "the parts location"
local cd = Instance.new("ClickDetector", part)
local isClicked = false
 
-- This function updates how the part looks based on a variable
local function updateVisuals()
	if isClicked == false then
		part.BrickColor = BrickColor.new("Bright red")
		part.Material = Enum.Material.DiamondPlate
        isClicked = true
	else
		part.BrickColor = BrickColor.new("Bright yellow")
		part.Material = Enum.Material.Wood
        isClicked = false
	end
end
 
-- Update, then start listening for clicks
updateVisuals()
cd.MouseClick:Connect(updateVisuals)
2 Likes

If this is in a local script it might not work. Also you need a click detector.

2 Likes

My script creates a click detector in the runtime. Also yes, this should be done in a script (script NOT localscript) in the serverscript service.

(Edit: @SimpForLua your username isn’t appropriate for the devforum, you should probably change it before you get banned. )

2 Likes

didnt work got an error in the output

image

for @RobloxTitan4223’s solution, the string is just a placeholder for the object’s directory, change it to the object’s place like workspace.Part etc

2 Likes

i dont know what you mean image

what i gotta do on there

do

local part = [directory]

for example, if I have a part in workspace then I would do local part = workspace.Part
or if i have a part in ReplicatedStorage I would do local part = game:GetService("ReplicatedStorage").Part

when referencing objects, you have to give the location of where they are
like I did earlier with local cd = script.Parent

1 Like

alright its works of somekind cause it just makes it red without even clicking it but let me explain the thing i wanna do :
image
image

Hello, you can do this. Put a click detector in the part you want to change. Then put a script in the click detector. You can then type this(“I’ll explain the code later”) Name the receiver “receiver”

script.Parent.MouseButton1Click:Connect(function())
if workspace.receiver.Material == Enum.Material.Neon then
workspace.reviever.Material = Enum.Material.SmoothPlastic
elseif workspace.receiver.Material == Enum.Material.SmoothPlastic then
workspace.reviever.Material = Enum.Material.Neon
end
end

When the script’s parent(In this case the ClickDetector) is clicked, it’ll run something. If the reciever’s Material is Neon, then turn it to smooth plastic. Else if it’s smooth plastic, turn it into neon.

Yay I posted my third reply :smiley:

2 Likes

something for this would be

local reciever = [directory] -- the reciever
local button = [directory] -- the button

local cd = button:FindFirstChildOfClass("ClickDetector") or Instance.new("ClickDetector") -- find a click detector, if it can't find one, it will make one
if not cd.Parent then cd.Parent = button end -- if the script created a clickdetector then it will set it's parent to the button

local recieverIsEnabled = false -- default value for the reciever
local neon = Enum.Material.Neon -- the neon material
local smoothPlastic = Enum.Material.SmoothPlastic -- the smoothplastic material

cd.MouseClick:Connect(function(player)
    if not recieverIsEnabled then -- if the reciever is disabled
        recieverIsEnabled = true -- set the value of "recieverIsEnabled to true"
        reciever.Material = neon -- set the reciever's material to neon
    else
        recieverIsEnabled = false
        reciever.Material = smoothPlastic
    end
end)

Edit: fixed a few typos

3 Likes

it would be script or local script and where do i put it? and can you make it that is multiple parts?