Material Changing Script Not Functioning

Hello developers!

I am trying to create a material changing part for my game. It is supposed to have a flicker effect by changing the material of the part between concreate and neon when the player clicks on the part.

Unfourunetly when I go in game my script does not seem to be working. Not even the click detector appears. It just stays concreate.

image
image
image

local Material = part.Material == Enum.Material


local detector = Instance.new("ClickDetector")
detector.Parent = part


local function updateVisuals()
	if part.Material == Enum.Material.Concrete then
		
		part.BrickColor = BrickColor.new("Yellow flip/flop")
		part.Material = Enum.Material.Neon
	else
		
		part.BrickColor = BrickColor.new("Bright yellow")
		part.Material = Enum.Material.Concrete
	end
end

local function onToggle()
	
	Material = not Material

	
	updateVisuals()
end


updateVisuals()
detector.MouseClick:Connect(onToggle)

All help is apreciated!

I have just tested it out, and it works perfectly.

Tho, why are you creating a ClickDetector instead of having one already inside the part?

--Variables
local part = script.Parent
local Material = part.Material == Enum.Material
local detector = part.ClickDetector


--Functions
local function updateVisuals()
	if part.Material == Enum.Material.Concrete then
		part.BrickColor = BrickColor.new("Yellow flip/flop")
		part.Material = Enum.Material.Neon
	else
		part.BrickColor = BrickColor.new("Bright yellow")
		part.Material = Enum.Material.Concrete
	end
end

local function onToggle()
	Material = not Material
	updateVisuals()
end


--Logic
updateVisuals()
detector.MouseClick:Connect(onToggle)

image

It is working now! I Added the click detector to the part.

(Also I am not sure if this was effecting it but I was using a local script and also the part was a union.)

Thanks!

Try to avoid unions in the future. They are a hell to work with.

1 Like

The use of a local script would be an issue as local scripts are not ran if placed inside the ‘Workspace’ container.