Im trying to get a rando object fro ma folder, and it just returns that its not containing a number in the folder.
heres the code
local ObjFolder = game.ReplicatedStorage:WaitForChild("SpawnTables")
local Spawns = script.Parent
task.wait(5)
for _, SpawnPad in pairs(Spawns:GetChildren()) do
if SpawnPad.Name == "ItemSpawn" then
if Class.Value == "Common" then
randomroom = ObjFolder.Common[math.random(1,#ObjFolder.Common:GetChildren())]
local ClonedRoom = randomroom:Clone()
ClonedRoom.Parent = game.Workspace
ClonedRoom:MoveTo(SpawnPad.Position)
ClonedRoom:PivotTo(SpawnPad.CFrame)
elseif Class.Value == "Rare" then
randomroom = ObjFolder.Rare[math.random(1,#ObjFolder.Rare:GetChildren())]
local ClonedRoom = randomroom:Clone()
ClonedRoom.Parent = game.Workspace
ClonedRoom:MoveTo(SpawnPad.Position)
ClonedRoom:PivotTo(SpawnPad.CFrame)
elseif Class.Value == "Legendary" then
randomroom = ObjFolder.Legendary[math.random(1,#ObjFolder.Legendary:GetChildren())]
local ClonedRoom = randomroom:Clone()
ClonedRoom.Parent = game.Workspace
ClonedRoom:MoveTo(SpawnPad.Position)
ClonedRoom:PivotTo(SpawnPad.CFrame)
end
end
end
When i looked online, all the answers said “put :getchildren()” but i already do that
(Also it says cloned room cus i took it from my other game, witch the script DOES work in, just not in this new game.)
What I’m confused about is that you say you’re trying to get a random object to spawn, but your script doesn’t seem that way. It’s kind of hard to explain. Can you tell me which line is giving errors?
You could change this:
spawn pad is an instance, and the script basically finds the class name (the rarity of the spawns) then grabs a random item from the folder and spawns (clones) it.
This same script was used in another game i made, and it worked fine. only diffrence is the location of the script.
Alright, so, the problem is quite simple, yet easy to miss due to the clusters and jumbles of code that you have.
Basically, the issue is in here:
the brackets [] basically serve the same function as a period ., translating your code would probably make it more understandable, so i’ll assign a random number for you:
randomroom = ObjFolder.Common[math.random(1, 5)]
As you see, what this line of code does is look inside an INSTANCE for a random number instead of an ARRAY or Table…
To fix this, simply do as such:
local room = ObjFolder.Common:GetChildren()
random_number = math.random(1,#ObjFolder.Common:GetChildren())
local randomroom = room[random_number]
Please do reply if this helps, and do not be ashamed for making such a simple mistake!
local ObjFolder = game.ReplicatedStorage:WaitForChild("SpawnTables")
local Spawns = script.Parent
task.wait(5)
for _, SpawnPad in pairs(Spawns:GetChildren()) do
if SpawnPad.Name == "ItemSpawn" then
if Class.Value == "Common" then
randomroom = ObjFolder.Common:GetChildren()[math.random(1,#ObjFolder.Common:GetChildren())]
local ClonedRoom = randomroom:Clone()
ClonedRoom.Parent = game.Workspace
ClonedRoom:MoveTo(SpawnPad.Position)
ClonedRoom:PivotTo(SpawnPad.CFrame)
elseif Class.Value == "Rare" then
randomroom = ObjFolder.Rare:GetChildren()[math.random(1,#ObjFolder.Rare:GetChildren())]
local ClonedRoom = randomroom:Clone()
ClonedRoom.Parent = game.Workspace
ClonedRoom:MoveTo(SpawnPad.Position)
ClonedRoom:PivotTo(SpawnPad.CFrame)
elseif Class.Value == "Legendary" then
randomroom = ObjFolder.Legendary:GetChildren()[math.random(1,#ObjFolder.Legendary:GetChildren())]
local ClonedRoom = randomroom:Clone()
ClonedRoom.Parent = game.Workspace
ClonedRoom:MoveTo(SpawnPad.Position)
ClonedRoom:PivotTo(SpawnPad.CFrame)
end
end
end