Zombie Spawning not working

Im working on a zombie spawning system but its not working, is there anything wrong with my code? The ClickDetectors are working but thats about all.

Script inside serverscriptservice:

local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventsFolder = ReplicatedStorage:WaitForChild("Events")

local ZombieFolder = Workspace:WaitForChild("ZombieFolder")
local ZombieEvent = EventsFolder:WaitForChild("ZombieEvent")

local spawning = false

function spawnZombie()
	local zombieTemplates = script:WaitForChild("ZombieTemplates"):GetChildren()
	
	local pickedZombie = zombieTemplates[math.random(1, #zombieTemplates)]:Clone()
	if pickedZombie.Name == "Male" then
		print("Male Picked")
	elseif pickedZombie.Name == "Female" then
		print("Female Picked")
	end
	
	local Spawners = ZombieFolder:WaitForChild("Spawners"):GetChildren()
	
	for _,spawner in pairs(Spawners:GetChildren()) do
		local pickedSpawn = Spawners[math.random(1, #Spawners)]
				pickedZombie.Parent = ZombieFolder:WaitForChild("Zombies")
				pickedZombie:SetPrimaryPartCFrame(pickedSpawn.CFrame)
				break
		end
	
end


function stopZombies()
	for _,zombie in pairs(ZombieFolder:WaitForChild("Zombies"):GetChildren()) do
		if zombie:FindFirstChild("Humanoid") then
			zombie:Destroy()
		end
	end
end

ZombieEvent.OnServerEvent:Connect(function(player, value)
	if value == "Start" then
		spawning = true
		spawn(function()
			while spawning do
				spawnZombie()
				wait(5)
			end
		end)
	elseif value == "Stop" then
		spawning = false
		stopZombies()
	end
end)

localscript inside starterplayerscripts:

local Workspace = game:GetService("Workspace")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local EventsFolder = ReplicatedStorage:WaitForChild("Events")
local ZombieEvent = EventsFolder:WaitForChild("ZombieEvent")
local ZombieFolder = Workspace:WaitForChild("ZombieFolder")


for _, part in pairs(ZombieFolder:GetChildren()) do
	if part.Name == "StartButton" or part.Name == "StopButton" then
		part.ClickDetector.MouseClick:Connect(function()
			if part.Name == "StartButton" then
				ZombieEvent:FireServer("Start")
			elseif part.Name == "StopButton" then
				ZombieEvent:FireServer("Stop")
			end
		end)
	end
end

Are there any errors? Are the remote events working? Test it by printing before the if statement.

No errors, it just doesnt work

Did you try adding print() after sending the event, and after receiving the event? Does the RemoteEvent actually run?

Just tried, the remote event isnt working

Is it firing the event though?

No i dont think so as the printing isnt working:

ZombieEvent.OnServerEvent:Connect(function(value)
	print("Event Fired")

I am not sure what the problem is, but why don’t you directly detect clicks on the server instead of the client, without the need to use RemoteEvents?

I tried to before, i used :FireAllClients, didnt work, then used :FireServer, but you can only fireserver from a client

I mean instead of using a client script, can’t you directly detect a click on the server? I am pretty sure click detectors work on the server as well.

I tried like this in the serverscriptservice script:

for _, button in pairs(ZombieFolder:GetChildren()) do
	if button.Name == "StartButton" or button.Name == "StopButton" then
		button.ClickDetector.MouseClick:Connect(function()
			if button.Name == "StartButton" then
				ZombieEvent:FireServer("Start")
			elseif button.Name == "StopButton" then
				ZombieEvent:FireServer("Stop")
			end
		end)
	end
end

and i got this:
“FireServer can only be called from the client”

You don’t need to do FireServer, what you could do is either use a BindableEvent or directly put it in the same script as the other, and set spawning to true/false

Ok, I dont remember what I had before because I did try this and it didnt work, so i dont remember why i changed it, but I did it and fixed a couple bugs and its working, thanks for your help!!!

1 Like

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