How can i make a random enemy spawn in a random location?

Basically I made a server script that spawns a random enemy (In a folder) in a random location but for some reason it doesn’t work
here is my code

local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

local EnemyType = math.random(1, #Enemies)
while true do
	game.ReplicatedStorage.Enemies[EnemyType].Position = Vector3.new(math.random(-500, 500), 10, math.random(-500, 500))
	game.ReplicatedStorage.Enemies[EnemyType].Parent = workspace
	wait(1)
end

but in the output it shows the error " 2 is not a valid member of Folder “ReplicatedStorage.Enemies”"
also the number is randomised its not always 2

Hi
Chance local EnemyType line to

local EnemyType =  Enemies[math.random(1, #Enemies)]
-- Then Set the Enemy CFrame equal to EnemyType
1 Like

ServerScriptService.SpawnEnemies:6: invalid argument #2 (string expected, got Instance)

1 Like
local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

local EnemyType = math.random(1, #Enemies)
while true do
	local NewEnemy = game.ReplicatedStorage.Enemies[EnemyType]:Clone()
	NewEnemy.Position = Vector3.new(math.random(-500, 500), 10, math.random(-500, 500))
	wait(1)
end

You forgot your Vector3 value when setting the position.

1 Like

5 is not a valid member of Folder “ReplicatedStorage.Enemies”

1 Like

I edited the script in the post to add vector3 values

1 Like

Try this solution:

local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

local EnemyType = math.random(1, #Enemies)
while true do
	local NewEnemy = Enemies[EnemyType]:Clone()
	NewEnemy.Position = Vector3.new(math.random(-500, 500), 10, math.random(-500, 500))
	wait(1)
end

The error is because it does it as if I were to call Enemies["5"], meaning it becomes Enemies.5. Also, you called from ReplicatedStorage, without using :GetChildren() to grab the instances and put them in an array.

3 Likes

Position is not a valid member of Model “RobloxEmployeeZombie”
It did pick a random zombie but the position doesn’t
also all the zombies are models so i think i will just set them a primary part

1 Like

Try this code

local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

local EnemyType = math.random(1, #Enemies)
while true do
	local NewEnemy = Enemies[EnemyType]:Clone()
	NewEnemy.HumanoidRootPart.Position = Vector3.new(math.random(-500, 500), 10, math.random(-500, 500))
	wait(1)
end
local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

local EnemyType = math.random(1, #Enemies)
while true do
	local NewEnemy = Enemies[EnemyType]:Clone()
	NewEnemy:MoveTo(Vector3.new(math.random(-500, 500), 10, math.random(-500, 500)))
	wait(1)
end

Another fix because I forgot that you have to use MoveTo().

1 Like

Hey bro your code works i just had to adjust some things but never the less thanks for help
the changes I made:

local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

while true do
	local EnemyType = math.random(1, #Enemies)
	local NewEnemy = Enemies[EnemyType]:Clone()
	NewEnemy.PrimaryPart.Position = Vector3.new(math.random(-300, 300), 10, math.random(-300, 300))
	NewEnemy.Parent = workspace
	wait(1)
end
2 Likes

You’re welcome. But, as a suggestion, use while wait(1) do instead:

local Enemies = game.ReplicatedStorage.Enemies:GetChildren()

while wait(1) do
	local EnemyType = math.random(1, #Enemies)
	local NewEnemy = Enemies[EnemyType]:Clone()
	NewEnemy:MoveTo(Vector3.new(math.random(-300, 300), 10, math.random(-300, 300)))
	NewEnemy.Parent = workspace
end

Also, MoveTo() moves the PrimaryPart, with the model aswell.

1 Like

ok then i will do that bye now i gotta go

2 Likes