Script duplicating twice bug

How do I fix it when the zombie spawn script duplicates the zombies. It happens after every second game I’m not sure why it duplicates.
On the first game it works normally but then on the second zombies spawn twice at the same time and I’m not sure what’s causing it.

Spawn Script:

script.Parent.Transparency = 1
local debounce = false
game.ReplicatedStorage.Events.spawnZombies.Event:Connect(function()
	if debounce == false and script.Parent.ready.Value == true then
		debounce = true
		script.Parent.Transparency = 0
		script.Parent.ParticleEmitter.Enabled = true
		wait(3)
		if script.Parent.zombieType.Value == "Zombie" then
			local Rzombies = game:GetService("ReplicatedStorage").Zombies.RegularZombies:GetChildren()
			local randomrZombie = Rzombies[math.random(1,#Rzombies)]:Clone()
			randomrZombie.Parent = workspace.World
			randomrZombie.HumanoidRootPart.CFrame = script.Parent.spawnPart.CFrame
			script.Parent.Transparency = 1
			script.Parent.ParticleEmitter.Enabled = false
		end
		if script.Parent.zombieType.Value == "Hunter" then
			local Rzombies = game:GetService("ReplicatedStorage").Zombies.Hunter:GetChildren()
			local randomrZombie = Rzombies[math.random(1,#Rzombies)]:Clone()
			randomrZombie.Parent = workspace.World
			randomrZombie.HumanoidRootPart.CFrame = script.Parent.spawnPart.CFrame
			script.Parent.Transparency = 1
			script.Parent.ParticleEmitter.Enabled = false
		end
		if script.Parent.zombieType.Value == "Boomer" then
			local Rzombies = game:GetService("ReplicatedStorage").Zombies.Boomer:GetChildren()
			local randomrZombie = Rzombies[math.random(1,#Rzombies)]:Clone()
			randomrZombie.Parent = workspace.World
			randomrZombie.Torso.CFrame = script.Parent.spawnPart.CFrame
			script.Parent.Transparency = 1
			script.Parent.ParticleEmitter.Enabled = false
		end
		if script.Parent.zombieType.Value == "FireZombie" then
			local Rzombies = game:GetService("ReplicatedStorage").Zombies.FireZombie:GetChildren()
			local randomrZombie = Rzombies[math.random(1,#Rzombies)]:Clone()
			randomrZombie.Parent = workspace.World
			randomrZombie.Torso.CFrame = script.Parent.spawnPart.CFrame
			script.Parent.Transparency = 1
			script.Parent.ParticleEmitter.Enabled = false
		end
		if script.Parent.zombieType.Value == "FireBoss" then
			local Rzombies = game:GetService("ReplicatedStorage").Zombies.Bosses
			local randomrZombie = Rzombies["Fire Boss"]:Clone()
			randomrZombie.Parent = workspace.World
			randomrZombie.Torso.CFrame = script.Parent.spawnPart.CFrame
			script.Parent.Transparency = 1
			script.Parent.ParticleEmitter.Enabled = false
		end
		script.Parent.ready.Value = false
		wait(3)
		debounce = false
	end
end)

Here is the game script if that has anything to do with it:

local alivePlrs = {}

start_game_clone = game.Workspace.World:Clone()
start_game_clone.Parent = game.Lighting


function isEmpty(t)
	for i, v in t do
		return false
	end
	return true
end

game.ReplicatedStorage.Events.EndGame.Event:Connect(function()
	game.ReplicatedStorage.Events.Zombies.Value = 0
	script.Game.Value = false
	game.ServerScriptService.Missions.Mission1.Story.Enabled = false
	wait(0.1)
	game.ServerScriptService.Missions.Mission1.Story.Enabled = true
	print("Game Over")
	for i,v in pairs(workspace.Music:GetChildren()) do
		if v:IsA("Sound") then
			v:Stop()
		end
	end
	if game.ReplicatedStorage.Events.Win.Value == true then
		
	else
		workspace.Sound.GameOverSound:Play()
		game.ReplicatedStorage.Events.GameOverText:FireAllClients("GAME OVER")
		wait(8)
		game.ReplicatedStorage.Events.GameOverText:FireAllClients("")
	end
	table.clear(alivePlrs)
	wait(5)
	workspace.World:Destroy()
	wait(1)
	game.Lighting.World:Clone().Parent = workspace
	local newScript = script:Clone()
	newScript.Parent = workspace
	wait(0.1)
	script:Destroy()
end)

game.Players.PlayerAdded:Connect(function(player)
	local v = Instance.new("BoolValue")
	v.Name = "Playing"
	v.Parent = player
	
	local v2 = Instance.new("BoolValue")
	v2.Name = "Voted"
	v2.Parent = player
end)

game.ReplicatedStorage.Events.voteMission.OnServerEvent:Connect(function(player, MissionValue, Mission)
	Mission.Value = Mission.Value + 1
	-- MissionValue.Number.Text = Mission.Value
	wait(15)
	game.ReplicatedStorage.Events.Mission1.Value = 0
	game.ReplicatedStorage.Events.Mission2.Value = 0
	game.ReplicatedStorage.Events.Mission3.Value = 0
end)
local TweenService = game:GetService("TweenService")
local tweenInfo = TweenInfo.new(
	10, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.Out, -- EasingDirection
	0, -- RepeatCount (when less than zero the tween will loop indefinitely)
	false, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)

while true do
	wait(5)
	workspace.Music.LobbyMusic:Play()
	game.ReplicatedStorage.Events.LobbyText:FireAllClients("The game will start soon..")
	wait(10)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients("Voting will begin in")
	wait(2)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients("3")
	wait(1)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients("2")
	wait(1)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients("1")
	wait(1)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients(" ")
	game.ReplicatedStorage.Events.voteScreen:FireAllClients()
	wait(6)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients("Voting is about to end.")
	wait(4)
	game.ReplicatedStorage.Events.LobbyText:FireAllClients(" ")
	game.ReplicatedStorage.Events.Fade:FireAllClients()
	wait(4)	
	workspace.Music.LobbyMusic:Stop()
	
	local Events = game.ReplicatedStorage.Events
	

	if Events.Mission1.Value > Events.Mission2.Value and Events.Mission3.Value then
		game.ReplicatedStorage.Events.Mission1Start:Fire()
	end
	
	if Events.Mission2.Value > Events.Mission1.Value and Events.Mission3.Value then
		game.ReplicatedStorage.Events.Mission2Start:Fire()
	end
	
	if Events.Mission3.Value > Events.Mission1.Value and Events.Mission2.Value then
		game.ReplicatedStorage.Events.Mission3Start:Fire()
	end
	
	if Events.Mission1.Value == Events.Mission2.Value and Events.Mission3.Value then
		game.ReplicatedStorage.Events.Mission1Start:Fire()
	end
	script.Game.Value = true
	for i, plr in pairs(game.Players:GetPlayers()) do
		if plr.Character.Humanoid.Health == 0 then
			if isEmpty(alivePlrs) then
				game.ReplicatedStorage.Events.EndGame:Fire()
			end
		else
			plr.Playing.Value = true
			game.ReplicatedStorage.Events.Guns:Fire()
			--	plr.Character.HumanoidRootPart.CFrame = workspace.TeleportDestination.CFrame
			table.insert(alivePlrs, plr)
			print(alivePlrs)
			plr.Character.Humanoid.Died:Connect(function()
				plr.Playing.Value = false
				plr.Medic.Value = false
				plr.Sheriff.Value = false
				table.remove(alivePlrs, alivePlrs[plr])
				print(alivePlrs)
				if isEmpty(alivePlrs) then
					game.ReplicatedStorage.Events.EndGame:Fire()
				end
			end)
		end
	end
	game.Players.PlayerRemoving:Connect(function(plr)
		print(plr.Name)
		plr.Playing.Value = false
		plr.Medic.Value = false
		plr.Sheriff.Value = false
		table.remove(alivePlrs, alivePlrs[plr])
		print(alivePlrs)
		if isEmpty(alivePlrs) then
			game.ReplicatedStorage.Events.EndGame:Fire()
		end
	end)
	wait(1293182391238123912312312313123)
end

Yeah so any help to fix it would be appreciated.

3 Likes

Where is the script that fires the;

game.ReplicatedStorage.Events.spawnZombies.Event:Connect(function()

1 Like

Can you print some text every time the remote is fired and send the result with the desired amount of zombies

1 Like

It’s a main story script in serverscriptservice

1 Like

The same thing is happening with a script that gives the weapons but the way that works is when there’s more than 1 player the weapons given to everyone will duplicate. Also made the same way the zombie spawn thing is so I thought i’d just say that.

Also I just did the print thing you said and it prints it twice when it duplicates

1 Like

are both local scripts?

1 Like

No its serverscript using bindable events

My best guess is something wrong with the script that fires the bindable events, also if they are server scripts consider storing the bindable events on server storage

1 Like

I don’t see anything wrong tho I fired the event properly and im not sure why it would duplicate like that. ill still try do the serverstorage thing

1 Like

I would guess the event fires more than once not the script itself cause there is a race condition

1 Like

How would that be fixed and yeah it is the event and not the script now that I realise

1 Like

Well with my sleep-deprived brain, all I can think of is to change connect to once and re-do the once connection and also add a 0.5~ delay or so, remove the debounce, and it should work. not a good suggestion though before attempting to implement this please wait for someone else with brain activity to reply if no one replies try the janky solution

1 Like

Or instead of duplicating them in the NPC script itself try replacing the bindable event with a for loop that iterates through all NPCs and duplicates them one by one

1 Like

Can you post the script that fires the remote event?

1 Like

The whole script is too long so ill shorten it

game.ReplicatedStorage.Events.Mission1Start.Event:Connect(function()
    game.ReplicatedStorage.Events.Guns:Fire()
    game.ReplicatedStorage.Events.spawnZombies:Fire()
end)