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

Why won't the headlights on both of my cars turn on/off with my script?

Asked by 7 years ago

I've decided to modify a headlight script I made to make adding cars with working head lights more simple.

Head Light script, placed in Server Script Service.

local sportscar1 = game.workspace.Cars.Sportscar1:GetChildren()
local sportscar2 = game.workspace.Cars.Sportscar2:GetChildren()
while true do

    for _, child in ipairs(sportscar1, sportscar2) do
        if child.Name == "Headlight" then
            if game.Lighting:GetMinutesAfterMidnight() == 6 * 60 then
                child.Material = Enum.Material.Plastic
                child.PointLight.Enabled = false
            elseif game.Lighting:GetMinutesAfterMidnight() == 18 * 60 then
                child.Material = Enum.Material.Neon
                child.Pointlight.Enabled = true
            end
        end
    end

    wait(.1) -- The time before the loop restarts- a tenth of a second.

end

I tried to use a table, but it seems to skip to the second item in the table automatically meaning the headlights on sports car1 do not work.

All of this could probably be avoided if I knew how to get the GetChildren function to search models in workspace or a folder for the part named "Headlight" Since the "Headlight" is in a model, the GetChildren doesn't detect the parts. That is what I originally tried, If I knew how to do that, not only would it make this a lot more simple, but it would cut on the time needed to add working headlights to car models.

So how can I get both car's headlights to function with a small amount of code like this so that adding more cars won't be a hassle?

Thank you in advance.

0
Before i can help, do you know where the cars are being stored in the workspace e.g. have a set folder or do you not know where theya are in the workspace User#5423 17 — 7y
0
They are in the Cars folder I have created. Dekadrachm 50 — 7y

Answer this question