I tried to make a script that will loop through a table of players, and if a player is not in the table, or died, they would get items back. But instead of that, a random player will get the items and not the player who had just died.


for x = 300,0,-1 do
	for m,plr in pairs(game.Players:GetPlayers()) do
		if plr then
			if plr.Character then
				if plr.Character.Humanoid.Health > 0 then
					if not table.find(plrs,plr) then

						print(plr.Name.." was not found in plrs table.")

						table.insert(plrs,plr)

						local index = table.find(plrs,plr)
						local target = plrs[index]

						print(target.Name)

						print(table.find(plrs,plr))

						local weapons = game.ServerStorage:WaitForChild("weapons")

						local melee = weapons:WaitForChild("Melee"):GetChildren()
						local ranged = weapons:WaitForChild("Ranged"):GetChildren()
						local misc = weapons:WaitForChild("Misc"):GetChildren()

						local chosenMelee = melee[math.random(1,#melee)]:Clone()
						local chosenRanged = ranged[math.random(1,#ranged)]:Clone()
						local chosenMisc = misc[math.random(1,#misc)]:Clone()

						chosenMelee.Parent = target.Backpack
						chosenRanged.Parent = target.Backpack
						chosenMisc.Parent = target.Backpack

						target.Character.Humanoid.Died:Connect(function()
							table.remove(plrs,index)
						end)
					else
						--player already in table
					end
				end
			else
				table.remove(plrs,m)
			end
		else
			table.remove(plrs,m)
		end
	end
end