Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Is it possible to change a parts material through an if-then statement?

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)

1 answer

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

1 comment

0

Hmm, so I tried that in a separate part and it worked just fine, but in the code I'm using I'm trying to turn two parts Neon when clicked. Maybe I incorrectly used the GetDescendants() function? Also thank you for responding! :)

This discussion is preserved as it was. New answers, comments, and votes are closed.