Script doesn't loop

I am making a game script but when all players die it does work once but then after again it doesn’t do anything when everyone dies anyone know why?

Handler:

local Players = game:GetService("Players")
local RoundModule = require(script.Parent.Parent.RoundModule)

Players.PlayerAdded:Connect(function(player)
	local playing = Instance.new("BoolValue")
	playing.Parent = player
	playing.Name = "Playing"
	playing.Value = false
	local baldi = Instance.new("BoolValue")
	baldi.Parent = player
	baldi.Name = "Baldi"
	baldi.Value = false
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character.Humanoid
	humanoid.Died:Connect(function()
		if workspace.Values.Game.Value == true and player.Playing.Value == true then
			player.Playing.Value = false
			workspace.Values.Players.Value = workspace.Values.Players.Value - 1
		end
	end)
end)

Players.PlayerRemoving:Connect(function(player)
	if player.Playing.Value == true and workspace.Values.Game.Value == true then
		workspace.Values.Players.Value = workspace.Values.Players.Value - 1
	end
end)

debounce = false

while true do
	repeat wait() until workspace.Values.Players.Value == 0
	if debounce == false and workspace.Values.Game.Value == true then
		debounce = true
		local descendants = game.Workspace:GetDescendants()
		for i,v in pairs(descendants) do
			if v:IsA("Sound") then
				v.Playing = false
			end
		end
		RoundModule.end_game()
	end
end

Module:

local module = {}

function module.end_game()
	print("The game has ended.")
	workspace.Values.Game.Value = false
	print(workspace.Values.Players.Value)
	workspace.Music["Ahh, fresh meat!"]:Play()
	script.Parent.GameScript:Destroy()
end

return module

Autofix:

wait(5)
local GameScript = game.ServerScriptService.GameScript:Clone()
GameScript.Parent = game.Lighting

while true do
	repeat wait() until not game.ServerScriptService:FindFirstChild("GameScript")
	GameScript:Clone().Parent = game.ServerScriptService
	wait(5)
end

Anyone know why this happens? And the Handler is in the GameScript btw.

You never set debounce back to false in the handler

doesn’t work and the gamescript is cloned from lighting and so is the handler script so it should restart. but even if i did make debounce false it didnt work

1 Like

Have you even tried it? (Char limit)

yes
and doesnt work

while true do
	repeat wait() until workspace.Values.Players.Value == 0
	if debounce == false and workspace.Values.Game.Value == true then
		debounce = true
		local descendants = game.Workspace:GetDescendants()
		for i,v in pairs(descendants) do
			if v:IsA("Sound") then
				v.Playing = false
			end
		end
		RoundModule.end_game()
		debounce = false
	end
end

Not sure if this makes a difference but try

u wrote nothing?

character limit character limit character limit

Try not setting the cloned scrip parent until the original is deleted

the script waits until the gamescript deletes and then clones it from lighting

here

wait(5)
local GameScript = game.ServerScriptService.GameScript:Clone()
GameScript.Parent = game.Lighting

while true do
	repeat wait() until not game.ServerScriptService:FindFirstChild("GameScript")
	GameScript:Clone().Parent = game.ServerScriptService
	wait(5)
end

It glitched for some reason.

if not debounce or (the other option) then
— your code here
end

I just found out now the reason why it’s not doing it again is because the humanoid died function is inside the playeradded function so it only works once. i fixed it by putting humanoid died in the script that changes value and now it loops.

example:

script.IniciateValue.OnServerEvent:Connect(function(player)
	player.Playing.Value = true
	player.Character.Humanoid.Died:Connect(function()
		if workspace.Values.Game.Value == true and player.Playing.Value == true then
			player.Playing.Value = false
			workspace.Values.Players.Value = workspace.Values.Players.Value - 1
		end
	end)
end)
1 Like