I'm really confused because sometimes the script works but most of the time it breaks and I dont know why. No errors are given in the output. The problem is that a random player ("The beast") should be assigned to team "Beast" but most of the time this does not happen. When I look in the chat it says "You're now on team "Beast" ". However, after that the chat says "You're now on team Survivor". So the randomplayer gets assigned to beast but after that it gets assigned to team "Survivor". Could someone help me? Thanks in advance

Code:

local beastPlayer = {}
local players = {}







for i, plrs in pairs(game.Players:GetPlayers()) do
		
	   if plrs then
			
			table.insert(players,plrs)
		end
	end
	
	
	local randomPlayer = players[math.random(1,#players)]
	table.insert(beastPlayer,randomPlayer)
	
	
	
	for i, beast in pairs(beastPlayer) do
		
		
		
		if beast and beast.Character then
			beast.TeamColor = BrickColor.new("Really red")
			table.remove(players,i)
			beast.Character:FindFirstChild("HumanoidRootPart").CFrame = 
                        spawnPoints[1].CFrame
			table.remove(spawnPoints,1)
			
			local beastInGame = Instance.new("BoolValue")
			beastInGame.Value = true
			beastInGame.Name = "BeastInGame"
			beastInGame.Parent = beast.Character
			
			local clonedScythe = 
                         game.ReplicatedStorage.Weapons:WaitForChild("Scythe"):Clone()
			clonedScythe.Parent = beast.Backpack
			
               else
			--nothing
			end
		end
    

	for i , player in pairs(players) do
		
		if player and player.Character then
			
			player.TeamColor = BrickColor.new("Really black")
			player.Character:FindFirstChild("HumanoidRootPart").CFrame = 
                        spawnPoints[1].CFrame
			table.remove(spawnPoints,1)
			
			local inGameValue = Instance.new("BoolValue")
			inGameValue.Name = "InGame"
			inGameValue.Value = true
			inGameValue.Parent = player.Character
			
			
			local clonedSword = 
                        game.ReplicatedStorage.Weapons:WaitForChild("Sword"):Clone()
			clonedSword.Parent = player.Backpack
			
		
		
		else
			table.remove(players,i)
		end
	end
	

1 comment

0

is beast.Character sometimes nil?