Function will not let me spawn a random NPC from folder

I wish to have a random character spawned into the Customers folder in workspace, doing this throws the error that the model in the folder does not exist. Another problem that happened is that the script does not call the function to make the NPC move towards the end, it seems to not even attempt to start the function. Specifically naming the character in the folder spawns the NPC if I were to try doing that.

The Script:

local customs = game.ServerStorage.Customers:GetChildren()
local randomChild = customs[math.random(1, #customs)]
local customer = require(script.Customer)


customer.Spawn(randomChild)

The portion of the ModuleScript that I have been having trouble with.

function customer.Spawn(name)
	local mobExists = ServerStorage.Customers:FindFirstChild(name)
	

	if mobExists then
		for i=1, 1 do
			task.wait(0.5)
			local newMob = mobExists:Clone()
			print("Character Spawned")
			newMob.HumanoidRootPart.CFrame = waypoints.Spawner.CFrame
			newMob.Parent = workspace.Customers
			newMob.HumanoidRootPart:SetNetworkOwner(nil)
			newMob.Humanoid.Died:Connect(function()
				task.wait(0.5)
				newMob:Destroy()
			end)



			coroutine.wrap(customer.Move)
		end


	else
		warn("The Requested Mob Does Not Exist:", name)
	end
end



return customer

Any help is appreciated!

2 Likes

try

customer.Spawn(randomChild.Name)

other than that the children may not all be loaded for game.ServerStorage.Customers:GetChildren()

could call this and the random in the module script when the function runs which then the children should be loaded

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.