Get half of the server players?

So uhh, I was making a script where it gets half of the server and kills them. But, I don’t really know how to get half of the server. So, how can I grab half of the servers player?

Thanks, Vaelkarr.

2 Likes
  1. Get all of the players
  2. Iterate through half of them
-- 1
local allPlayers = game:GetService("Players"):GetPlayers()

-- 2
for i = 1, #allPlayers/2 do
	local player = allPlayers[i]
	-- get player's character and kill it
end

If you want it to be random, you would have to iterate through them, pick a random index, and pop them with table.remove:

local RNG = Random.new()
-- 1
local allPlayers = game:GetService("Players"):GetPlayers()

-- 2
for _ = 1, #allPlayers/2 do
	local i = RNG:NextInteger(1, #allPlayers)
	local player = table.remove(allPlayers, i)
	-- get player's character and kill it
end
3 Likes

Thanks! I tested the first one out, it worked.

2 Likes

Quick tip:

game:GetService("Players"):GetPlayers()

is more efficient than:

 game:GetService("Players"):GetChildren()
3 Likes

I completely forgot that was a function, much obliged!

For some reason, it stopped working. I don’t know why. Can you help?

for i = 1,#grabplayers/2 do
    i.Character.Humanoid:TakeDamage(i.Character.Humanoid.MaxHealth)
     wait(10)
     script.Parent.Visible = false
end

Also, I put down a print under the for statement, but it didn’t print. So, I think it’s something along the first line.

i would be a number, not a player, so you would have to index grabplayers with i to get the player.

Why this is true

The type of for loop you are using here is a numerical for loop, meaning it will run whatever is inside the indented body once for every number between the start and finish value inclusively:

for index = start, finish do
	body()
end

grabplayers is an array of players starting with an index of 1 and going up to the total number of players. #grabplayers returns how many players are in the array. This would mean that having a numerical for loop that starts at 1 and finishes at #grabplayers will give you an index for every element within grabplayers that you can use to get the player at that index:

-- setup values
local start = 1
local finish = #grabplayers

-- for loop
for index = start, finish do
	local player = grabplayers[ index ]
	print(index, player)
end

So in theory, if you halve the finish number, you also halve how many players you are looping through:

-- setup values
local start = 1
local finish = #grabplayers

-- for loop
for index = start, finish / 2 do
	local player = grabplayers[ index ]
	print(index, player)
end

You should also put the wait(10) after the for loop, not inside, otherwise it will wait 10 seconds for every player looped through and make whatever script.Parent is invisible an unnecessary amount of times.

Improved code:

for i = 1, #grabplayers/2 do
	grabplayers[i].Humanoid.Health = 0
end

wait(10)
script.Parent.Visible = false

If you just want to get half of the players (or any table), but not kill them, you can use this

local AllPlayers = game:GetService("Players"):GetPlayers()
local HalfPlayers = table.move(AllPlayers, 1, math.ceil(#AllPlayers/2), 1, {})
3 Likes

Just a small addition, incase of a server with an odd amount of players, your code would iterate through 1 - (a number and a half).

to fix this, I would add either math.ceil() or math.floor(). Example:
if there were 7 players in your server, if you used math.floor(), half would round down to 3 instead of 3.5, however if you used math.ceil() half would round up to 4 instead of 3.5.

local RNG = Random.new()
-- 1
local allPlayers = game:GetService("Players"):GetPlayers()

-- 2
for _ = 1, math.floor(#allPlayers/2) do -- this would round the value down
	local i = RNG:NextInteger(1, #allPlayers)
	local player = table.remove(allPlayers, i)
	-- get player's character and kill it
end

OR

local RNG = Random.new()
-- 1
local allPlayers = game:GetService("Players"):GetPlayers()

-- 2
for _ = 1, math.ceil(#allPlayers/2) do -- this would round the value up
	local i = RNG:NextInteger(1, #allPlayers)
	local player = table.remove(allPlayers, i)
	-- get player's character and kill it
end

EDIT: Totally didnt see @Minstrix 's post, but its the same concept

1 Like

Just to add, the main reason for using one over the other would be how you want the code to handle one player. Let’s say there is only one player in-game. Do you want them to die? If so, use math.ceil() (which rounds 0.5, half of 1, to 1) and if not, use math.floor() (which would round the 0.5, half of 1, to 0). As well, using math.ceil() will result in more players dying if there is an uneven number of players online. Which one you use will depend on how you want your game to play.

2 Likes