Can't set variable to table item?

I’ve recently been working on a light flicker script for my Backrooms game and am currently simply working on functions to toggle lights. Here’s my code:

print("Script Started")
lightFolder = game.Workspace.Map.Lights
lights = {}
print("Script Functioning")
activeLight = 0

for i in lightFolder:GetChildren() do
	table.insert(lights, i)
	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 in NumberRange(100) do
	lightOff(i)
end

and here’s the error I get when it reaches the final for loop. Does anyone know a way around this?

Which line is 31?

limit limit limit

It’s because NumberRange is a Data Type, not a function. A data type has constructors (having a function .new()) like Instance. NumberRange is commonly used in ParticleEmitters. A NumberRange doesn’t return a Tuple of numbers from 1 to 100.

I think you’re trying to iterate numbers from 1 to 100

for i = 1, 100 do
    lightOff(i)
end
1 Like

Like what the person said above, it would be
NumberRange.new(min, max)

That’s not how NumberRange works…

I suggest you read Creating Flickering Lights to learn more about how to make them. I previously tried out the tutorial myself and got good results :slightly_smiling_face:

That results in this error.

That tutorial is for a very different flickering to what I want to create. My goal is to have a script that occasionally selects a random set of lights to flicker for a short amount of time and also allows me to toggle lights on command (Developer trolling is a lot of fun hehe)

I also suggest you use CollectionService instead of a lights Folder both to be able to loop through them better and for better organization although you’ll still need to give each light a different name if you want to be able to toggle each light individually. In the end this is up to you to decide though. I’ll see if I have time today to make an example script if you wish

I don’t want to loop through them, I want the script to select random lights. I only have the for loop at the end for debugging purposes.

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
1 Like

random? you can use math.random() or Random:NextInteger() for that!

to flicker the lights, simply use lightOff() then lightOn() repeatedly

so instead of this

for i = 1, 100, 1 do
	lightOff(i)
end

you do this

local duration = 5 -- how long the flickering will take in seconds
local timePassed = 0 -- self explanatory

repeat
	math.randomseed()
	local i = math.random(1, #lights)
	
	lightOff(i)
	task.wait()
	lightOn(i)
	
	timePassed += task.wait()
until timePased >= 0 -- until the time to flicker has finished

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.