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

Material is not a valid member of Script?

Asked by 6 years ago

Hello, I'm just getting started and I'm very new to coding. I'd appreciate some light on my issue here.

Thanks!


Lights = script.Parent:GetChildren() for i, v in pairs(Lights) do if game.Lighting:GetMinutesAfterMidnight() > 6 * 60 then v.Material = Enum.Material.Plastic end if game.Lighting:GetMinutesAfterMidnight() < 18 * 60 then v.Material = Enum.Material.Neon -- It breaks here. end end

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

You are looping through an entire group (lighting) and changing a value that does not exist in most objects (v). As your error says, "Material" wasn't found in a 'script' object since they can only be found in 'part' objects. So to solve this, you have to do some error checking to make sure 'v' is a 'part' and not a 'script' and such. To check whether 'v' is a 'part', you can simply use this one command in an if statement:

if v:IsA("Part") do

and have that if statement placed as the first line within that for loop and edit the loop as needed to close off the if statements. Hopefully this helps you and happy scripting~

0
Thanks that helped remove the error and I understand how to use :IsA() now! But I decided to move the script to ServerScriptService so I dont have to copy and paste it in every single model and my code is still not working. :( DemiDragon 0 — 6y
0
And I made the Table/Variable Lights = game.Workspace.Lights:GetChildren() DemiDragon 0 — 6y
0
Aha, I figured it out, I just used a 'while true do' loop and now it works perfectly fine. DemiDragon 0 — 6y
Ad

Answer this question