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

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

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

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

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