i see, in line 9, you’re inserting the index number instead of the light instance in the table. if you don’t know what index is, when you’re using for in pairs() do loops, pairs() returns two values index (the order number of an object in an array, an array is a table where the key is a number) or key (the string name of an object in a dictionary, a dictionary is a table where the key is a string), and value (the object in the table).
-- THIS IS JUST AN EXAMPLE, NOT THE SOLUTION
local array = {
"Welcome",
"to",
"Roblox!"
}
for index, value in ipairs(array) do -- using ipairs() instead of pairs() in arrays will result in lowest to highest order in index and faster; ipairs() can only be used in arrays, not in dictionaries or combination of both
print(index, value)
end
-- the above should print:
-- > 1 Welcome
-- > 2 to
-- > 3 Roblox!
local anotherArray = {
[2] = "This",
[3] = "table",
[5] = "is",
[7] = "full",
[11] = "of",
[13] = "primes!"
}
for index, value in ipairs(anotherArray) do
print(value, index)
end
-- the above should print:
-- > This 2
-- > table 3
-- > is 5
-- > full 7
-- > of 11
-- > primes! 13
local dictionary = {
["Apple"] = "I love ",
Banana = "I love ",
["Corn"] = "I love ",
Dung = "I hate"
}
for key, value in pairs(dictionary) do -- when iterating dictionaries, always use pairs() instead of ipairs()
print(value .. key)
end
-- the above should print (might not be in order):
-- > I love Apple
-- > I love Banana
-- > I love Corn
-- > I hateDung
so the solution is to change for i in lightFolder:GetChildren() to for i, v in ipairs(lightFolder:GetChildren()) do then change the next line to lights[i] = v
print("Script Started")
lightFolder = game.Workspace.Map.Lights
lights = {}
print("Script Functioning")
activeLight = nil
for i, v in ipairs(lightFolder:GetChildren()) do
lights[i] = v
print("Light ", i, "was added to list")
end
function lightOn(id)
activeLight = lights[id]
activeLight.SurfaceLight.Enabled = true
activeLight.Color = Color3.fromRGB(211, 190, 150)
activeLight.Material = Enum.Material.Neon
print("Turned on light ", id)
end
function lightOff(id)
activeLight = lights[id]
activeLight.SurfaceLight.Enabled = false
activeLight.Color = Color3.fromRGB(132, 119, 94)
activeLight.Material = Enum.Material.SmoothPlastic
print("Turned off light ", id)
end
print("Light toggle functions loaded")
for i = 1, 100, 1 do
lightOff(i)
end