How do i make a randomize NPC Spawner

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

this is the Folder in Workspace
image

1 Like

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 #.

1 Like

so i have a Warning that said
“Infinite yield possible on 'Workspace:WaitForChild(“Instance”)”

1 Like

try this

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
2 Likes

My bad; Revert the code and do this instead:

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.

2 Likes

nope i got a error that said
“Workspace.RandomizeScript:5: attempt to get length of a Instance value”

1 Like

Try to copy it again because i just added something to the script

2 Likes

i think meant by this?

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
1 Like

i just got
" Workspace.RandomizeScript:5: Expected ‘)’ (to close ‘(’ at column 83), got ‘]’"

1 Like

maybe this will work

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
1 Like

man got the same result
“Infinite yield possible on 'Workspace:WaitForChild(“Instance”)”

1 Like

What’s in the ScaryDummysSpawnersFolder

1 Like

a bunch of parts that’s named ‘Spawner’

1 Like

Why do you need it in a while loop

1 Like

so the Script will continue to spawn the NPC in the ReplicatedStorage

1 Like

I’m almost done ignoreeeeeeeeee

1 Like

i will not ignore mwahahahaha😈

1 Like

lol does the game crash when you play it?

1 Like

no, but ill try to make a script that automatically deletes the previous Npc and adds a new one
just to make it less laggy

1 Like
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

that has to work

1 Like