Zombie spawn not working well

So im trying to make a zombie spawn system that all the zombies don’t spawn into the same position. This is my script right now

game.Lighting.Changed:Connect(function()
	if game.Lighting.ClockTime == 20 then
		for count = 1,33 do
	task.wait(1)
			local clone = game.ReplicatedStorage.zombies.Zombie:Clone()
			local folder = game.Workspace.ZombieSpawns:GetChildren()
			local random = folder[math.random(1, 33)]
			folder = game.Workspace.ZombieSpawns:GetChildren()
			clone:MoveTo(random.Position)
			clone.Parent = workspace
			random.Parent = game.ReplicatedStorage.DisableSpawns
			wait(1)
			random = folder[math.random(1, 33)]
			
end
	end
end)

Tell me if u know how to fix


game.Lighting.Changed:Connect(function()
	if game.Lighting.ClockTime == 20 then
		for count = 1,33 do
	task.wait(1)
			local clone = game.ReplicatedStorage.zombies.Zombie:Clone()
			local folder = game.Workspace.ZombieSpawns:GetChildren()
			local random = folder[math.random(1, 33)]
			folder = game.Workspace.ZombieSpawns:GetChildren()
			clone:SetPrimaryPartCFrame(random.CFrame)
			clone.Parent = workspace
			random.Parent = game.ReplicatedStorage.DisableSpawns
			wait(1)
			random = folder[math.random(1, 33)]
			
end
	end
end)

try this

ServerScriptService.SpawnZombie:9: attempt to index nil with 'CFrame'
It sometimes doesn’t give that error and works and other time it gives that error and breaks

Is “random” a part or is it a model?

The random is a model of a zombie

1 Like

Wait, so is it a separate part/model, like a spawnpoint for the zombie?

Oooh sorry, the random is a spawnpoint for the zombie and the zombie is in replicated storage as a model, but the random is not a model

game.Lighting.Changed:Connect(function()
	if game.Lighting.ClockTime == 20 then
		for count = 1,33 do
	task.wait(1)
			local clone = game.ReplicatedStorage.zombies.Zombie:Clone()
			local folder = game.Workspace.ZombieSpawns:GetChildren()
			local random = folder[math.random(1, #folder)]
			folder = game.Workspace.ZombieSpawns:GetChildren()
			clone:SetPrimaryPartCFrame(random.CFrame)
			clone.Parent = workspace
			random.Parent = game.ReplicatedStorage.DisableSpawns
			wait(1)
			random = folder[math.random(1, #folder)]
			
end
	end
end)

Yeah, then the ‘random’ should have a CFrame by default. Try this new script.

1 Like

Waitt I think I see the problem. Random is an operator (or something)
Use another variable.


game.Lighting.Changed:Connect(function()
	if game.Lighting.ClockTime == 20 then
		for count = 1,33 do
	task.wait(1)
			local clone = game.ReplicatedStorage.zombies.Zombie:Clone()
			local folder = game.Workspace.ZombieSpawns:GetChildren()
			local rng = folder[math.random(1, #folder)]
			folder = game.Workspace.ZombieSpawns:GetChildren()
			clone:SetPrimaryPartCFrame(rng.CFrame)
			clone.Parent = workspace
			rng.Parent = game.ReplicatedStorage.DisableSpawns
			wait(1)
			rng = folder[math.random(1, #folder)]
			
end
	end
end)

this should do it lol

Just edited it because I made a typo, if it errors, try recopying this code

1 Like

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