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