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

How do I use collection service for multiple parts to use the same script?(answered)

Asked by 1 year ago
Edited 1 year ago

Im trying to make a script that makes multiple parts turn and off during a day/night cycle, and I added collection service to tag the parts. I used the tag editor plugin and it still doesnt seem to work. I keep getting the error message "ServerScriptService.Script:14: attempt to index nil with 'Enabled'. The script is located in server script service, here it is:

local lighting = game:GetService("Lighting")

local currenttime = 4
local collectionService = game:GetService("CollectionService")
local Lamps = collectionService:GetTagged("Lamps")
while true do
    lighting.ClockTime = currenttime

    if (currenttime >= 6 and currenttime <= 19) then
        Lamps.Material = Enum.Material.Plastic
        Lamps.PointLight.Enabled = false
    else
        Lamps.Material = Enum.Material.Neon
        Lamps.PointLight.Enabled = true
    end

    currenttime += .01
    if currenttime >= 24 then
        currentTime = 0
    end
    wait()
end

1 answer

Log in to vote
0
Answered by 1 year ago
Edited 1 year ago

I have solved the problem. I inserted a for loop to check through the tagged instances. Here is the result:

local lighting = game:GetService("Lighting")

local currenttime = 4
local collectionService = game:GetService("CollectionService")
local Lamps = collectionService:GetTagged("Lamps")
while true do
    for _, Lamp in ipairs(Lamps) do
        lighting.ClockTime = currenttime

        if (currenttime >= 6 and currenttime <= 19) then
            Lamp.Material = Enum.Material.Plastic
            Lamp.PointLight.Enabled = false
        else
            Lamp.Material = Enum.Material.Neon
            Lamp.PointLight.Enabled = true
        end

        currenttime += .01
        if currenttime >= 24 then
            currentTime = 0
        end
        wait()
    end

end
Ad

Answer this question