Hello! I am trying to use this script down below to clone a model and put it into workspace. Now I haven’t gotten to cloning yet because each time I try to find the model (In a folder in ServerStorage) with the name chosen by the math.random, it returns with a nil value on line 16.
I have all of the models named correctly as the print statements print(penguinsTable) and print(chosenPenguin) both output the full table and the chosen name from the table.
Script in ServerScriptService
local ss = game:GetService("ServerStorage")
local penguinsFolder = ss:WaitForChild("Penguins")
local penguinsTable = {}
for i, v in pairs(penguinsFolder:GetChildren()) do
--print(v.Name)
table.insert(penguinsTable, v)
end
print(penguinsTable)
local function spawnPenguin()
local chosenPenguin = penguinsTable[math.random(#penguinsTable)]
print(chosenPenguin)
local clone = penguinsFolder:FindFirstChild(chosenPenguin) <---- returning a nil value
print(clone)
if clone then
print(chosenPenguin, " found!")
end
end
while true do
wait(2)
spawnPenguin()
end
What I have tried!
Modified Script
local ss = game:GetService("ServerStorage")
local penguinsFolder = ss:WaitForChild("Penguins")
local penguinsTable = {}
for i, v in pairs(penguinsFolder:GetChildren()) do
--print(v.Name)
table.insert(penguinsTable, v)
end
print(penguinsTable)
local function spawnPenguin()
local chosenPenguin = penguinsTable[math.random(#penguinsTable[1])]
print(chosenPenguin[1])
local clone = penguinsFolder:FindFirstChild(chosenPenguin[1]) <---- returns, "[1] is not a valid member of model.
print(clone)
if clone then
print(chosenPenguin, " found!")
end
end
while true do
wait(2)
spawnPenguin()
end
Thanks in advance!!