So I am making a police car and I want this script to pick random “lights” from the lightbar model and make them visible for 0.1 seconds.
When I enable the siren, I get this error:
Players.LMVM2041.PlayerGui.SirenControl.Frame.Sirens.Wail.Script:12: attempt to get length of a Instance value
My script:
local wail = workspace["A-Chassis Suspension 3500HD"].DriveSeat.Wail
local lights = workspace["A-Chassis Suspension 3500HD"].Body.LIGHTBAR.Lights
local db = false
script.Parent.MouseButton1Click:Connect(function()
wail:Play()
while wait(0.2) do
for _, lights in pairs(lights:GetChildren()) do
if lights:IsA("BasePart") then
local random1 = lights[math.random(#lights)]
local random2 = lights[math.random(#lights)]
local random3 = lights[math.random(#lights)]
random1.Transparency = 0
random2.Transparency = 0
random3.Transparency = 0
wait(0.1)
random1.Transparency = 1
random2.Transparency = 1
random3.Transparency = 1
end
end
end
end)
The issue here is that you’re trying to use the # on an Instance. # only works on strings and tables. The code you want to have is:
local children = lights:GetChildren()
while task.wait(0.2) do
-- removed the 'for loop.' Judging by your script, it wasn't necessary
for _ = 1, 3 do
--a loop is much more better because it does a lot of the work for you
--so you wouldn't have to change/create more variables for a limited number of objects
--if you want to change how many lights change, edit '3' to another number
--the '_' means that the current iteration index is not of use/going to be used
task.spawn(function()
--this creates a new separate thread so that other iterations won't have to wait for each other to finish
local light = children[math.random(#children)]
if (not light:IsA("BasePart")) then
--skips the iteration if the current object is not a light
continue
end
light.Transparency = 0
task.wait(0.1)
light.Transparency = 1
end)
end
end
-- it's also better practice to use the 'task' library
Keep in mind, this is for the sake of what you’re trying to accomplish. You’d have to modify the code yourself if you don’t want a chance of the same light being chosen.