Help with round system script

Hello scripters! I have been working on a new game that uses a round system. The script I have doesn’t work very well and I don’t know why. So I am wondering if I should redo the script entirely using a different method, or if you could help me find bugs.

Current Script

local status = game.ReplicatedStorage.Status
local gunStorage = game.ServerStorage.Guns
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local length = 30
local DataStore2 = require(game.ServerScriptService.Data.DataStore2)

while true do
	
	status.Value = "Waiting for players..."
	
	repeat wait(1) until Players.NumPlayers > 1
	
	status.Value = "Intermission.."
	
	local currentPlrs = {}
	local pTable = Players:GetChildren()
	local random1 = math.random(1,#pTable)
	
	for i, v in pairs(pTable) do
		if i == random1 then
			table.insert(currentPlrs, i, v)
			table.remove(pTable, i)
		end
	end
	
	local random2 = math.random(1,#pTable)
	
	for i, v in pairs(pTable) do
		if i == random2 then
			table.insert(currentPlrs, i, v)
			table.remove(pTable, i)
		end
	end
	
	wait(20)
	
	status.Value = "Duel between "..currentPlrs[1].Name.." and "..currentPlrs[2].Name.." beginning..."
	
	for i,v in pairs(currentPlrs) do
		character = game.Workspace:FindFirstChild(v.Name) 
		local aSpawn = workspace.Spawns:FindFirstChild(i)
		character:FindFirstChild("HumanoidRootPart").CFrame = aSpawn.CFrame
		print(currentPlrs[1],currentPlrs[2], currentPlrs[3])
		
		local newGun = gunStorage:FindFirstChild(v.gunSlot.Value):Clone(); newGun.Parent = v.Backpack
	end
	
	for i = length, 0, -1 do
		
		for i, v in pairs(currentPlrs) do
			v.character.Humanoid.Died:Connect(function()
				table.remove(currentPlrs, i)
			end)
		end
		
		if #currentPlrs == 1 then
			status.Value = currentPlrs[1].Name.." has won!"
			local moneyStore = DataStore2("money", currentPlrs[1])
			moneyStore:Increment(10)
			break
		elseif #currentPlrs == 0 then
			status.Value = "No one won :("
			break
		elseif i == 0 then
			status.Value = "Time up."
		end
	end

end

If you see any noticeable bugs could you point them out. There are no errors in the output.

If you do redo the script entirely, i would 100% use module scripts. They keep everything in order and if one system breaks, you can easily fix it because of how easy it is to find and access the faulty lines of code.

Hope this helped! :+1:

1 Like

I have never thought of using module scripts for it. Might be more reliable. One problem is that I don’t really want to redo the entire script. I know it is only like 70 lines, but I don’t see how I would change it.

I was just saying if you were to redo the entire script. Anyway, ill try to help you fix the problem. Any errors in the output? And what do you mean by ‘doesn’t work very well’?Does it work? Does it feel unstable? Does it fail sometimes?

No errors. What is happening is the script chooses 2 players and then continues to teleport them to the spawns during the round. Whenever a player dies nothing happens, the infinite loop of being teleported continues.

I haven’t tested with 3 players, but I am pretty sure it should teleport only 2 people.