function mainModule.GenerateStop()
for i, v in pairs(RSStopStorage:GetChildren()) do
local randomStop = v[math.random(1, #v)] -- Error Here
randomStop.Parent = WSStopStorage
BE.NewStop:Fire()
mainModule.TweenGlass(1, MainSubway.LeftDoor.Glass, Color3.fromRGB(152, 194, 219), 0.75)
mainModule.TweenGlass(1, MainSubway.RightDoor.Glass, Color3.fromRGB(152, 194, 219), 0.75)
task.wait(1)
mainModule.TweenDoors(1, MainSubway.LeftDoorOutDestination, MainSubway.LeftDoor.Door)
mainModule.TweenDoors(1, MainSubway.RightDoorOutDestination, MainSubway.RightDoor.Door)
Values.Stops.Value += 1
Values.AtStop.Value = true
end
end
When I try and localize randomStop in output it always says: "attempt to get length of a Instance value".
(I also just tried puttinglocal randomStop = math.random(1, #v)but it still says the same thing in output.)
I assume it’s because the things inside RSStopStorage are parts, and not numbers.
Hope this was enough information!
Any help is appreciated!
You’re trying to use #v, which is the length operator, on an instance value. The # operator only works on tables that have sequential integer keys, which is why you’re getting the “attempt to get length of Instance value” error message.
replace #v with table.getn(v) or table.maxn(v). Both table.getn() and table.maxn() return the size of the table (the number of elements in a sequence table), which should work for your use case.
So your code should look something like:
local randomStop = v[math.random(1, table.getn(v))]
or
local randomStop = v[math.random(1, table.maxn(v))]