Please help me solve the problem

for the whole day I tried to solve the problem, but all to no avail. for some reason, my game broke down. I made a script to issue random weapons, but no weapons are issued, and the game stops the timer when the player dies on the playing field.
here is an example script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local lobby = game.Workspace.Spawn
local maps = ReplicatedStorage.Maps:GetChildren()
local status = ReplicatedStorage.Status
local duelteam = game.Teams.Duelists
local lobbyteam = game.Teams.Lobby
local giveweapons = 1
local weapons = game.ReplicatedStorage.Weapons
local remote = game.ReplicatedStorage.PickWeapon
local function giveweapon (playerweapon)
	for i = 1, giveweapons, 1 do
		while true do
			local rand = math.random(1, #weapons:GetChildren())
			local randonweapon = weapons:GetChildren()[rand]
			if not playerweapon.Backpack:FindFirstChild(randonweapon.Name) then
				randonweapon:Clone().Parent = playerweapon.Backpack
				break
			end
		end
	end
end
remote.OnServerEvent:Connect(function(plr)
	giveweapons(plr)
end)
while true do
	local players = Players:GetPlayers()
	players.Team = lobbyteam
	if #players >= 2 then
		for i = 5, 0, -1 do
			status.Value = "Intermission:" .. i
			wait(1)
		end
		local chosenmap = maps[math.random(1, #maps)]
		local clonemap = chosenmap:Clone()
		clonemap.Parent = workspace
		status.Value = "Map:" .. clonemap.Name
		wait(2.5)
		local chosenplayer1 = players[math.random(1, #players)]
		local chosenplayer2 = nil
		repeat
			wait()
			chosenplayer2 = players[math.random(1, #players)]
		until chosenplayer2 ~= chosenplayer1
		chosenplayer1.Character:SetPrimaryPartCFrame(clonemap.Teleport1.CFrame)
		chosenplayer1.Team = duelteam
		chosenplayer2.Character:SetPrimaryPartCFrame(clonemap.Teleport2.CFrame)
		chosenplayer2.Team = duelteam
		chosenplayer1.Character:WaitForChild("Humanoid").Died:Connect(function()
			chosenplayer1.Team = lobbyteam
		end)
		chosenplayer2.Character:WaitForChild("Humanoid").Died:Connect(function()
			chosenplayer2.Team = lobbyteam
		end)
		for i = 10, 0, -1 do
			status.Value = "Liquidation:" .. i
			local playing = {}
			if chosenplayer1.Team.Name == "Duelists" then
				table.insert(playing, chosenplayer1)
			end
			if chosenplayer2.Team.Name == "Duelists" then
				table.insert(playing, chosenplayer2)
			end
			if #playing == 0 then
				status.Value = "The duelists died"
				wait(3)
				break
			end
			if #playing == 1 then
				status.Value = playing[1].."Has WON!"
				wait(3)
				break
			end
			wait(1)
			if i == 0 then
				local sound = game.ReplicatedStorage["kid fav blinker"]
				sound:Play()
				wait(0.7)
				chosenplayer1.Character.Humanoid.Health = 0
				chosenplayer2.Character.Humanoid.Health = 0
				status.Value = "Explosive draw!"
				wait(3)
			end
		end
		clonemap:Destroy()
		chosenplayer1.Character:SetPrimaryPartCFrame(lobby.Tp1.CFrame)
		chosenplayer1.Team = lobbyteam
		chosenplayer2.Character:SetPrimaryPartCFrame(lobby.Tp2.CFrame)
		chosenplayer2.Team = lobbyteam
	else
		status.Value = "You're unlikely to play alone."
		wait(0.5) 
		status.Value = "You're unlikely to play alone.."
		wait(0.5)
		status.Value = "You're unlikely to play alone..."
		wait(0.5)
	end
end

here is another script for issuing weapons, if you need it

local character = script.Parent
local plr = game.Players:GetPlayerFromCharacter(character)
local remote = game.ReplicatedStorage.PickWeapon
if plr.Team == "Duelists" then
	remote:FireServer()
end

I will be very grateful if you help me solve the problem :slightly_smiling_face:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local workspace = game:GetService("Workspace")
local lobby = workspace:WaitForChild("Spawn")
local maps = ReplicatedStorage.Maps:GetChildren()
local status = ReplicatedStorage.Status
local duelteam = game.Teams.Duelists
local lobbyteam = game.Teams.Lobby
local giveweapons = 1
local weapons = ReplicatedStorage.Weapons
local remote = ReplicatedStorage.PickWeapon

local function giveweapon(player)
    for i = 1, giveweapons do
        while true do
            local rand = math.random(1, #weapons:GetChildren())
            local randomWeapon = weapons:GetChildren()[rand]

            if not player.Backpack:FindFirstChild(randomWeapon.Name) then
                randomWeapon:Clone().Parent = player.Backpack
                break
            end
        end
    end
end

remote.OnServerEvent:Connect(function(player)
    giveweapon(player)
end)

while true do
    local players = Players:GetPlayers()

    for _, player in pairs(players) do
        player.Team = lobbyteam
    end

    if #players >= 2 then
        for i = 5, 0, -1 do
            status.Value = "Intermission: " .. i
            wait(1)
        end

        local chosenmap = maps[math.random(1, #maps)]
        local clonemap = chosenmap:Clone()
        clonemap.Parent = workspace
        status.Value = "Map: " .. clonemap.Name
        wait(2.5)

        local chosenplayer1 = players[math.random(1, #players)]
        local chosenplayer2 = nil

        repeat
            chosenplayer2 = players[math.random(1, #players)]
        until chosenplayer2 ~= chosenplayer1

        chosenplayer1.Character:SetPrimaryPartCFrame(clonemap.Teleport1.CFrame)
        chosenplayer1.Team = duelteam
        chosenplayer2.Character:SetPrimaryPartCFrame(clonemap.Teleport2.CFrame)
        chosenplayer2.Team = duelteam

        local function handlePlayerDeath(player)
            player.Team = lobbyteam
        end

        chosenplayer1.Character:WaitForChild("Humanoid").Died:Connect(function()
            handlePlayerDeath(chosenplayer1)
        end)

        chosenplayer2.Character:WaitForChild("Humanoid").Died:Connect(function()
            handlePlayerDeath(chosenplayer2)
        end)

        for i = 10, 0, -1 do
            status.Value = "Liquidation: " .. i
            local playing = {}

            if chosenplayer1.Team == duelteam then
                table.insert(playing, chosenplayer1)
            end

            if chosenplayer2.Team == duelteam then
                table.insert(playing, chosenplayer2)
            end

            if #playing == 0 then
                status.Value = "The duelists died"
                wait(3)
                break
            end

            if #playing == 1 then
                status.Value = playing[1].Name .. " has WON!"
                wait(3)
                break
            end

            wait(1)

            if i == 0 then
                local sound = ReplicatedStorage:WaitForChild("kid fav blinker")
                sound:Play()
                wait(0.7)
                chosenplayer1.Character.Humanoid.Health = 0
                chosenplayer2.Character.Humanoid.Health = 0
                status.Value = "Explosive draw!"
                wait(3)
            end
        end

        clonemap:Destroy()
        chosenplayer1.Character:SetPrimaryPartCFrame(lobby.Tp1.CFrame)
        chosenplayer1.Team = lobbyteam
        chosenplayer2.Character:SetPrimaryPartCFrame(lobby.Tp2.CFrame)
        chosenplayer2.Team = lobbyteam
    else
        status.Value = "You're unlikely to play alone."
        wait(0.5)
        status.Value = "You're unlikely to play alone.."
        wait(0.5)
        status.Value = "You're unlikely to play alone..."
        wait(0.5)
    end
end
  • Updated the way teams are assigned to players.
  • Removed the redundant ‘character’ variable in the second script.
  • Used wait instead of while true to introduce delays.

Be sure to review my code and change anything you don’t want.

thank you very much for your help. But unfortunately the weapon is still not issued