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)
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)
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)
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)
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
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.
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)