Game not functioning as expected

Hi, I’m working on a zombie wave based game. I have all my main scripts, the game works but it has a few problems. I have a remaining zombie count but it sometimes goes down 1 time or 3 times and I don’t know why. Also only one zombie spawns in at one period of time, when it dies then it will wait for the 1-5 seconds and spawn another zombie in. Here are my scripts:
MainGameHandler located in serverscriptservice:

local roundMsg = game.ReplicatedStorage.Values:WaitForChild("roundMsg")
local zombieCount = game.ReplicatedStorage.Values:WaitForChild("ZombiesRemaning")
local zombiesAlive = game.ReplicatedStorage.Values:WaitForChild("ZombiesAlive")
local gameInProgress = game.ReplicatedStorage.Values:WaitForChild("gameInProgress")
local wave = game.ReplicatedStorage.Values:WaitForChild("Wave")
local collectionservice = game:GetService("CollectionService")


while true do
	for i = 15,0,-1 do
		roundMsg.Value = "Game Starting in: "..i
		if i == 1 then
			local map = math.random(1,3)
			if map == 1 then
				game.ReplicatedStorage.Maps.Map1:Clone().Parent = workspace.Map
			end
			if map == 2 then
				game.ReplicatedStorage.Maps.Map2:Clone().Parent = workspace.Map
			end
			if map == 3 then
				game.ReplicatedStorage.Maps.Map3:Clone().Parent = workspace.Map
			end
			wait(1)
			print(map)
			for i, v in pairs(game.Players:GetPlayers()) do
				collectionservice:AddTag(v.Character, "Alive")
				v.Character.UpperTorso.CFrame = workspace.Map:FindFirstChildOfClass("Model").Spawn.CFrame
			end
			--roundMsg.Value = "Game In Progress"
			zombieCount.Value = 10
			--zombiesAlive.Value = 6
			wave.Value = 1
			gameInProgress.Value = true

			repeat
				if #collectionservice:GetTagged("Alive") > 0 then
					if zombieCount.Value <= 0 then
						wave.Value = wave.Value + 1
						zombieCount.Value = 10 + wave.Value
						--zombiesAlive.Value = 6 * wave.Value
					end
				elseif #collectionservice:GetTagged("Alive") == 0 then
					gameInProgress = false
				end
				wait(1)
			until gameInProgress == false
			workspace.Map:ClearAllChildren()
		end
		wait(1)
	end
end

ZombieSpawner located in serverscriptservice:

while true do
	if game.ReplicatedStorage.Values.gameInProgress.Value == true then
		if game.ReplicatedStorage.Values.ZombiesRemaning.Value > 0 then
			--local NPC = game.ReplicatedStorage.Zombie:Clone()
			--NPC.Parent = script.Parent
			--NPC.UpperTorso.CFrame = script.Parent.CFrame
			local SpawnPoints = workspace.SpawnPoints:GetChildren()
			local NumberOfSpawnPoints = #SpawnPoints
			local RSP = math.random(1, NumberOfSpawnPoints)
			local ChosenSpawnPoint = SpawnPoints[RSP]

			--// Replicated Storage
			local RS = game:GetService("ReplicatedStorage")

			--// Zombies + Random Zombie
			local Zombies = RS:WaitForChild("Zombies"):GetChildren()
			local NumberOfZombies = #Zombies
			local RZ = math.random(1, NumberOfZombies)
			local ChosenZombie = Zombies[RZ]


			--// Cloned Random Zombie
			local ClonedZombie = ChosenZombie:Clone()
			local FS = ClonedZombie:WaitForChild("FollowScript")

			ClonedZombie.Parent = ChosenSpawnPoint
			--ClonedZombie.CFrame = ChosenSpawnPoint.CFrame
			ClonedZombie:PivotTo(ChosenSpawnPoint.CFrame)
			wait(1)
			FS.Enabled = true
			game.ReplicatedStorage.Values.ZombiesSpawned.Value = game.ReplicatedStorage.Values.ZombiesSpawned.Value + 1
		end
	end
		local wave = game.ReplicatedStorage.Values.Wave
		local ZR = game.ReplicatedStorage.Values.ZombiesRemaning
		if ZR.Value == 10 + wave.Value then
		wait(10) 
		ZR = 0
	end
	for i = 1,5 do
		wait(i)
	end
end

ZombieDeathScript located in every zombie

local humanoid = script.Parent:WaitForChild("Humanoid")
local DeathAnim = humanoid.Animator:LoadAnimation(script.DeathAnimation)
local Zombie = script.Parent
local FollowScript = script.Parent.FollowScript
local ZR = game.ReplicatedStorage.Values.ZombiesRemaning
local ZA = game.ReplicatedStorage.Values.ZombiesAlive

humanoid.HealthChanged:Connect(function(Health)
	if Health <= 0 then
		ZR.Value = ZR.Value - 1
		--ZA.Value = ZA.Value - 1
		script.Parent.HumanoidRootPart.Anchored = true
		FollowScript.Disabled = true
		DeathAnim:Play()
		wait(3)
		Zombie:Destroy()
	end
end)

Video of the problem (Sorry for the lag, it was fine for me):
Zombie.wmv (5.6 MB)

To answer your first problem, you should use Humanoid.Died instead of Humanoid.HealthChanged since your method will always fire when the zombie’s health changes and thus if you were to damage the zombie when it’s dead, the event will fire causing the zombie countdown to be brought down. While Humanoid.Died will only fire a single time when the zombie dies.

You don’t need an operator to check the zombie’s health

Humanoid | Roblox Creator Documentation

For your second problem, in the spawner script you have a for loop which is yielding your script and thus makes the zombie spawn at 1 - 5 seconds. I hope this helps

1 Like

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