My Code is not working properly

Hey Fellow developers I’m working on a game a there’s a problem with my script first of all the player wont respawn when they die or leave the Plr table 0n death also they player who is the last one standing wont win the game when the game is over.

Can anyone help me fix/improve my code if you need me to explain more just ask
thanks :slight_smile:

local Replicatedstorage = game:GetService("ReplicatedStorage")

local Serverstorage = game:GetService(“ServerStorage”)
local MapsFolder = Serverstorage:WaitForChild(“Maps”)
local NeededPlayers = 2
local Status = game.Workspace.Values.Status
local Gamelenght = 60
local IntermissionTime = 10

while true do
Status.Value = “Waiting For Enough Players!”
repeat wait(1) until game.Players.NumPlayers >= NeededPlayers
Status.Value = “Intermission”
wait(IntermissionTime)

local plrs = {}

for i , player in pairs(game.Players:GetPlayers()) do
	if player then 
		table.insert(plrs,player)
	end
end

wait(2)

local AvalibleMaps = MapsFolder:GetChildren()

local ChosenMap = AvalibleMaps[math.random(1,#AvalibleMaps)]  

local ClonedMap = ChosenMap:Clone()
local Gun = Serverstorage.Gun.Gun
ClonedMap.Parent = game.workspace

Status.Value = ChosenMap.Name.." Map Chosen!"
wait(3)
local Spawnpoints = ClonedMap:FindFirstChild("SpawnPoints")
local AvalibleSpawnPoints = Spawnpoints:GetChildren()
local GunSpawnpoints = ClonedMap:FindFirstChild("GunSpawns")
local Children = GunSpawnpoints:GetChildren()
local ChosenGunSpawn = Children[math.random(1, #Children)]
Gun.CFrame = ChosenGunSpawn.CFrame
Gun.Parent = game.Workspace

function OnTouch(Part)
	if Part.Parent ~= nil and Part.Parent:FindFirstChild("Humanoid") then
		local RealGun = game.ReplicatedStorage.Weapons.HandGun
		local GunClone = RealGun:Clone()
		GunClone.Parent = game.Players:findFirstChild(Part.Parent.Name).Backpack
		Gun:Destroy()
	end
end



for i, player in pairs(plrs) do 
	if player then
		character = player.Character
		if  character then 
			character:FindFirstChild("HumanoidRootPart").CFrame = AvalibleSpawnPoints[1].CFrame
			table.remove(AvalibleSpawnPoints,1)
			local gametag = Instance.new("BoolValue")
			gametag.Name = "GameTag"
			gametag.Parent = player.Character
		else
			if not player then
				table.remove(plrs,i)

			end
		end
	end
end
Status.Value = "Find The Gun!"	

wait(2)

for i = Gamelenght,0,-1 do
	
	Gun.Touched:Connect(OnTouch)

	for x, player in pairs(plrs) do
		if player then

			character = player.character

			if not character then
			else
				if character:FindFirstChild("GameTag") then
				else
					table.remove(plrs,x) 
				end
			end
		else
			table.remove(plrs,x)

		end
	end
	Status.Value = i
	if #plrs == 1 then
		Status.Value = "The Winner Is "..plrs[1].Name.."!"
		plrs[1].leaderstats.Cash.Value = plrs[1].leaderstats.Cash.Value + 1
		wait(4)
		break
	elseif i == 0 then
		Status.Value = "Times Up"
		break
	end

	wait(1)
end

for i, player in pairs(game.Players:GetPlayers()) do
	character = player.character

	if not character then
	else
		if character:FindFirstChild("GameTag") then
			character.GameTag:Destroy()

		if character:FindFirstChild("HandGun") then
		end
			character.HandGun:Destroy()
		end

		if character:FindFirstChild("HandGun") then
			character.Gun:Destroy()
		end

	end

player:LoadCharacter()

end

ClonedMap:Destroy()
Gun:Destroy()

Status.Value = “Game Ended”

end

Have you used any free models in the game? If there’s an infected model, it could be preventing the player from respawning.

Hmm no all my models are made by me

Well one thing you shouldn’t do is using repeat inside a loop, that’s gonna end up lagging the server

Hmm ok what would you use instead?

Please check the output for errors and inform us about them. That’s common debugging!

This doesn’t seem to be the full script, is it?

just a normal loop

while true do
    if game.Players.NumPlayers >= NeededPlayers then
        --something
    end
end

Repeating is basically a loop in itself, so whenever a loop is making another loop, it’s gonna end up repeating that proccess over and over and it’s gonna lag the server.

ok thankyou I’m new to making game loops and information helps

that’s the weird thing the output is not saying anything

is NumPlayers a value?

no i just put it after players and it worked

image

Try putting .Value after this

ok that makes sense then i wont have to type .Value every time

What about the maps Clone part of the script is
there any thing I can improve?

Instead of destroying it, move it back into Storage, deleting parts will add memory

Ok but the only problem with that is that I have destroyable parts on the map that need to be replaced when the round is over