Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

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

Asked by 5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by
fredfishy 833 Moderation Voter
5 years ago
Edited 5 years ago

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

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! :) Beastlance 22 — 5y
Ad

Answer this question