Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

[solved] how do i randomly spawn zombies?

for i=1,10 do
	local x = math.random(1,100)
	local z = math.random(1,100)
	local zombie = game:GetService("ReplicatedStorage")["Drooling Zombie"]:Clone()
	zombie.Parent = game.Workspace
	zombie:PivotTo(CFrame.new(x,0,z))
end

here's my code, when i run it the zombies are spawned together in the same place instead of random positions, how do i fix this?

3 answers

I have this code in one of my older games so it's a bit sloppy and used for a specific purpose. Here you can have it.

local bool = game.ReplicatedStorage.value.roun -- boolvalue
local server = game:GetService("ServerStorage")
local drool = server.objects["Zm"] -- mob
local zombie = game.ReplicatedStorage.value.zombie -- zombie amount
local rounds = game.ReplicatedStorage.value.roundn -- round number
local total = game.ReplicatedStorage.value.TotalZ
bool.Changed:Connect(function(newval) -- checking if true
	if bool.Value == true then -- if value is true
		rounds.Value = rounds.Value + 1 -- add value to round
		local potato = zombie.Value -- just localizing zombie value
			potato = rounds.Value *4 -- changing value to rounds x 4
			total.Value = potato
		for i = 1,potato do  
			local drools = drool:Clone()-- cloning desired amount of zombies
			drools.Name = ("Zombie")
			drools.Parent = game.Workspace.Mobs
			if drools.Name ~= ("Zombie")then  -- chcking the name
				drools.Name = ("Zombie") -- changing name
				drools.PrimaryPart.CFrame = CFrame.new(math.random(-350,-156),.5,math.random(-236,-13)) -- random pos on map
				local parts = drools:GetChildren()
				parts.Position = Vector3.new(drools.PrimaryPart.Position)
			elseif drools.Name == ("Zombie") then 
					print ("ZFound") 
					end
			end
		end
	end)

	




Try this

for i=1,10 do
	locall zombie = game:GetService("ReplicatedStorage")["Drooling Zombie"]:Clone()
	zombie.Parent = game.Workspace
	zombie:PivotTo(CFrame.new(math.random(1,1000),0,math.random(1,1000)))
end

1 comment

0

that doesn't fix my problem, that just widens the scope of where they spawn. im trying to get them to spawn seperately in random positions. i thought each time a for loop ran, a different random number would be generated?

holy hell it took me way too long to solve this issue

for i=1,3 do
	local zombie = game:GetService("ReplicatedStorage")["Drooling Zombie"]:Clone()
	local x = Random.new():NextInteger(1,100)
	local z = Random.new():NextInteger(1,100)
	zombie.Parent = game.Workspace
	zombie:PivotTo(CFrame.new(x,0,z))
end

i fixed it using the new random library, apparently math.random() is deprecated so use the new random from here on out ig, anyways the post is here if anyone needs it in the future

This discussion is preserved as it was. New answers, comments, and votes are closed.