so I’m trying to make a script that can spawn a NPC Randomly
i keep getting a error saying
“Workspace.Script:5: attempt to get length of a Instance value”
this is the script i got so far
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder
while true do
local RandomTime = {"1","5"}
local RandomPos = game.Workspace:WaitForChild(SpawnName[math.random(1, #SpawnName)])
noob = game.ReplicatedStorage.AIFolder.ScaryDummy:clone()
noob.Parent = game.Workspace
noob:MoveTo(RandomPos.Position + Vector3.new(0,1,0))
print("Spawned!")
wait(RandomTime[math.random(1, #RandomTime)])
end
You are referencing an instance here; in line 5 you try to get its lenght using the # operator, which is impossible. To fix it, add :GetChildren().
The correct line should be:
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder:GetChildren()
This way it returns a table with all the instances and you can get the lenght using #.
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder
while true do
local RandomTime = {"1","5"}
local RandomPos = game.Workspace:WaitForChild(SpawnName:GetChildren()[math.random(1, #SpawnName:GetChildren()])
noob = game.ReplicatedStorage.AIFolder.ScaryDummy:clone()
noob.Parent = game.Workspace
noob:MoveTo(RandomPos.Position + Vector3.new(0,1,0))
print("Spawned!")
wait(RandomTime[math.random(1, #RandomTime)])
end
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder
while true do
local RandomTime = {"1","5"}
local RandomPos = game.Workspace:WaitForChild(SpawnName[math.random(1, #SpawnName:GetChildren()])
noob = game.ReplicatedStorage.AIFolder.ScaryDummy:clone()
noob.Parent = game.Workspace
noob:MoveTo(RandomPos.Position + Vector3.new(0,1,0))
print("Spawned!")
wait(RandomTime[math.random(1, #RandomTime)])
end
I added the :GetChildren() later instead of the first line, this way it should fix the error.
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder
while true do
local RandomTime = {"1","5"}
local RandomPos = game.Workspace:WaitForChild(SpawnName[math.random(1, #SpawnName)]:GetChildren())
noob = game.ReplicatedStorage.AIFolder.ScaryDummy:clone()
noob.Parent = game.Workspace
noob:MoveTo(RandomPos.Position + Vector3.new(0,1,0))
print("Spawned!")
wait(RandomTime[math.random(1, #RandomTime)])
end
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder
while true do
local RandomTime = {"1","5"}
local RandomPos = game.Workspace:WaitForChild(SpawnName:GetChildren()[math.random(1, #SpawnName:GetChildren())])
noob = game.ReplicatedStorage.AIFolder.ScaryDummy:clone()
noob.Parent = game.Workspace
noob:MoveTo(RandomPos.Position + Vector3.new(0,1,0))
print("Spawned!")
wait(RandomTime[math.random(1, #RandomTime)])
end
local SpawnName = game.Workspace.ScaryDummysSpawnersFolder
while true do
local RandomTime = {"1", "5"}
local RandomPos = SpawnName:WaitForChild(SpawnName:GetChildren()[math.random(1, #SpawnName:GetChildren())])
noob = game.ReplicatedStorage.AIFolder.ScaryDummy:clone()
noob.Parent = game.Workspace
noob:MoveTo(RandomPos.Position + Vector3.new(0,1,0))
print("Spawned!")
wait(RandomTime[math.random(1, #RandomTime)])
end