No idea why this script doesn't work

I’ve been working on a zombie PvE game and when trying to spawn in the zombies, I made this code.

function spawnzombie(zombie)
	local z = game.ReplicatedStorage.Zombies:FindFirstChild(zombie):Clone()
	z.Parent = workspace
	z:MoveTo(script.Parent.Spawn.Position)
end

count = 0
while true do
	wait(5)
	count = math.random(1,100)
	print(count % 2)
		if (count % 2) == 0 and not (count % 10) == 0 and not (count % 100) == 0 then
			spawnzombie("Heavy")
		elseif (count % 5) == 0 and not (count % 10) == 0 then
			spawnzombie("Giant")
		elseif (count % 10) == 0 and not (count % 100) == 0 then
			spawnzombie("Giant Heavy")
		elseif (count % 100 == 0) then
			spawnzombie("Dankis Zombie")
		else 
			spawnzombie("Zombie")
		end
end

Can anyone tell me why when the zombie is actually attempting to be spawned, ROBLOX Studio instantly crashes?
1 Like

With a quick skim, I’m not entirely sure why your code would cause studio to crash. Are you sure it’s definitely to do with this script? Are there no other possible causes?

p.s Just for future reference, it’s good practice to format your code properly with “```” - three of the weird dash things to the left of the number 1 on the keyboard.

so instead of:
while true do end

it becomes:

while true do end

It is line 5 of the script, the z:MoveTo () that crashes the game.

and by the way I’ve edited it so it looks better now

Mind posting the structure of your zombies?

The zombies are R15 models, just like the normal ROBLOX characters, nothing special.

inside a folder in ReplicatedStorage called Zombies

Don’t use Move to(). Set the LowerTorso of the zombie’s CFrame to the teleport position.

I’m not familiar with operating CFrame, How would you go about doing this?

Would SetPrimaryPartCFrame() work?

Creating a CFrame with just a position is easy.

local c = CFrame.new(Vector3 position)

But how do you set CFrame to a model?

A few problems I see here.

  1. I’m gonna assume the structure of your zombie is in a model. You want to use the function:
Model:SetPrimaryPartCFrame(CFrame.new(SpawnPosition))

CFraming is more accurate but it won’t account for collisions so make sure that the spawn positions are accurate.

  1. For your probabilities I recommend doing something like this instead:
local zombProb = {0.01, 0.1, 0.2, 0.5, 1}
local num = math.random()
for k = 1, #zombProb do
      if num < zombProb[k] then
              spawnzomb(k)
              break
      end
end

No since the primary part of a character model is the HumanoidRootPart

In general, SetPrimaryPartCFrame is frowned upon for it’s gradual offsets.

I was struggling with the probabilities so I decided to sequentially generate the zombies instead. Thanks for the suggestion though!

(And thanks (hopefully) for a solution too!)

Setting the primarypart cframe should work in his case if he is just spawning the zombie in once. I don’t recommend doing that for animating the zombie; you would use the humanoid:moveto functions.

Sorry but that doesn’t work!
It still crashes when it tries to place the zombie into the map

remove the while loop and try it again. See if there are any errors, or crashing