I was attempting to make a light switch in studio which when clicked would turn on a light and change the part material of the light to neon. Everything was working out fine until I got to changing the material through an if-then statement. Through multiple tests on a different part I determined that for whatever reason, it doesn't seem possible to change the material of a part through an if-then statement. Can anybody help me with either creating new code or bugfixing? Thank you!
Light switch code:
local sPC = script.Parent.ClickDetector local pointLight = script.Parent.Parent:FindFirstChild("Light"):FindFirstChild("PointLight") local glow = script.Parent.Parent:FindFirstChild("Light").neon:GetDescendants() local function hitDaLights(player) if pointLight.Enabled == true then pointLight.Enabled = false else pointLight. Enabled = true end end local function neon() for index, descendant in pairs(glow) do if glow.Material == "Neon" then glow.Material = "SmoothPlastic" else glow.Material = "Neon" end end end sPC.MouseClick:connect(hitDaLights) sPC.MouseClick:connect(neon)
Works for me if I use Enum.Material
rather than a string.
Instance.new("ClickDetector", script.Parent).MouseClick:connect(function() print(script.Parent.Material) print(script.Parent.Material == Enum.Material.Neon) if script.Parent.Material == Enum.Material.Neon then script.Parent.Material = Enum.Material.SmoothPlastic else script.Parent.Material = Enum.Material.Neon end end)
Also, you probably don't want to bind the same event to two separate functions like that.
Maybe try
spc.MouseClick:connect(function(player) hitDaLights(player) neon(player) end)
or something