Script keeps repeating

i made a round system that starts when theres 2 players, but when i’m the only one in the server the script keeps repeating, how do i fix this?

local replicatedstorage = game:GetService("ReplicatedStorage")
local game_folder = replicatedstorage.game

local remotes_folder = game_folder.remotes
local values_folder = game_folder.values

local soundservice = game:GetService("SoundService")
local soundgroup = soundservice.soundsgroup

local plrs = game:GetService("Players")
local tweenService = game:GetService("TweenService")

local tInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
local tInfo2 = TweenInfo.new(0.4, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)

local Lobby_Music_Tween_1  = tweenService:Create(soundgroup.lobby_theme, tInfo, {Volume = 1.3})
local Lobby_Music_Tween_0  = tweenService:Create(soundgroup.lobby_theme, tInfo, {Volume = 0})

playersinRound = {}
killer = {}

required_player = 2

local function removePlayerfromRound(player)
	local index = table.find(playersinRound, player)
	if index then
		print(player, "has left the round")
		table.remove(playersinRound, index)
	end
end

game.Players.PlayerRemoving:Connect(removePlayerfromRound)

plrs.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		if player:FindFirstChild("values_folder") == nil then
			local new_folder = Instance.new("Folder", player)
			local isSurvivor_Value = Instance.new("BoolValue", new_folder)
			
			local isKiller_Value = Instance.new("BoolValue", new_folder)
			
			isSurvivor_Value.Name = "isSurvivor"
			isKiller_Value.Name = "isKiller"
			
			new_folder.Name = "values_folder"
		elseif player:FindFirstChild("values_folder") then
			return
		end
	end)
end)

plrs.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function()
		local player_values = player:WaitForChild("values_folder")
		
		player_values.isKiller.Changed:Connect(function()
			if player_values.isKiller.Value == true then
				local cloned_tag = game:GetService("ReplicatedStorage").killer_tag:Clone()
				cloned_tag.Parent = player.Character:WaitForChild("Head")
			elseif player_values.isKiller.Value == false then
				player.Character:WaitForChild("Head"):FindFirstChild("killer_tag"):Destroy()
			end
			
			player_values.isSurvivor.Changed:Connect(function()
				if player_values.isSurvivor.Value == true then
					local cloned_tag = game:GetService("ReplicatedStorage").survivor_tag:Clone()
					local character = player.Character
					
					cloned_tag.Parent = character:WaitForChild("Head")
				elseif player_values.isSurvivor.Value == false then
					local character = player.Character
					
					character:WaitForChild("Head"):FindFirstChild("survivor_tag"):Destroy()
				end
			end)
		end)
	end)
end)

local function system(player)
	wait(3)
	print("start again")
	values_folder.time.Value = 120
	
	soundgroup.lobby_theme:Play()
	
	local chosen = plrs:GetChildren()[math.random(1, #plrs:GetChildren())]
	
		values_folder.teleport.Value = false

		values_folder.blackout.Value = false
		values_folder.inRound.Value = false

		values_folder.can_show.Value = true
	
	values_folder.inLobby.Value = true
	
	values_folder.killer_user.Value = "none"
	
	Lobby_Music_Tween_1:Play()
	
	killer = {}
	
	soundgroup.tick:Play()
		
	local player = game:GetService("Players"):GetPlayers()[1]
	
	player:WaitForChild("values_folder").isSurvivor.Value = false
	chosen:WaitForChild("values_folder").isKiller.Value = false
	
	playersinRound = {}
	local connections = {}

	
	--repeat wait() until game.Players.NumPlayers >= required_player
	
	if game.Players.NumPlayers >= required_player then
		soundgroup.tick:Play()
		repeat wait(1) values_folder.time.Value -= 1; remotes_folder.change_text:FireAllClients(); remotes_folder.change_text:FireAllClients(); until
		values_folder.time.Value <= 0

		Lobby_Music_Tween_0:Play()
		values_folder.inLobby.Value = false

		if values_folder.time.Value == 0 then
			wait(1)
			values_folder.round_start.Value = true
			soundgroup.tick:Stop()
			print("round starting")
			soundgroup.clock_chime:Play()
		end

		wait(8)
		print("starting")
		
		values_folder.killer_user.Value = chosen.Name
		print("killer is "..values_folder.killer_user.Value)

		chosen:WaitForChild("values_folder").isKiller.Value = true
		chosen:WaitForChild("values_folder").isSurvivor.Value = false


		if player:WaitForChild("values_folder").isSurvivor.Value == false and player.values_folder.isKiller.Value == false then
			player:WaitForChild("values_folder").isSurvivor.Value = true
			print(player.Name.." is survivor")
		end


		wait(3)


		values_folder.time.Value = 120
		--values_folder.teleport.Value = true

		for i, player in pairs(game.Players:GetPlayers()) do
			if player.Character and player.Character:FindFirstChild("Humanoid") then
				local rootpart = player.Character:WaitForChild("HumanoidRootPart")
				rootpart.CFrame = workspace.game.spawn.CFrame
				table.insert(playersinRound, player)
				connections[player.Name] = player.Character.Humanoid.Died:Connect(function()
					if player:WaitForChild("values_folder").isSurvivor.Value == true then
						removePlayerfromRound(player)
					end
				end)

				if player:WaitForChild("values_folder").isKiller.Value == true then
					player.Character:WaitForChild("HumanoidRootPart").CFrame = workspace.game.killer_spawn.CFrame
				end

			end
		end

		wait(3.2)
		game_folder.values.inRound.Value = true

		repeat wait(1) values_folder.time.Value -= 1; remotes_folder.change_text:FireAllClients(); until
		values_folder.time.Value <= 0

		for i, connection in pairs(connections) do
			connection:Disconnect()
		end

		wait(4)
		
		for i, player in pairs(game.Players:GetPlayers()) do
			if player.Character and player.Character:FindFirstChild("Humanoid") then
				local rootpart = player.Character:WaitForChild("HumanoidRootPart")
				
				rootpart.CFrame = workspace.game.lobby_spawn.CFrame
				values_folder.blackout.Value = true
			elseif game.Players.NumPlayers >= 1 then
				return
			end
		end
	end
end

while true do
	task.wait()
	system()
end

first off why are there 2 ‘PlayerAdded’ events

the first playeradded event is for making the folder and the value and the second playeradded event is for checking if the player is a survivor or a killer

The issue might just be the 2 player added events. Try just using one and putting all the code in that

so i put the player added events in the function?

you could just do something like this

local function PlayerAddedEvent(Player)
--code goes here
end

game.Players.Playeradded:Connect(PlayerAddEvent)
1 Like

And the issue might just be checking if there are 2 players in the server, here is some code which should be easy to implement

repeat wait(1) until game.Players.NumPlayers >= 2
1 Like

Whoops seems like you already have that, my bad

i disabled that code lol, i’ll try enabling it wait

thank you so much it fixed the problem

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