Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How would i kill half the players in the game?

Asked by 6 years ago

Basically i have this it kills all the players but i dont have a clue on how i'd make this kill half of the players in the game at random

for i,v in pairs(game.Players:GetPlayers()) do
    v.Character.Humanoid.Health = 0
end

0
`if i <= #Players:GetPlayers()/2 then`? TheeDeathCaster 2368 — 6y
0
ATestAccount420, the robloxian thanos Le_Teapots 913 — 6y
0
omg how did u know ATestAccount420 31 — 6y

4 answers

Log in to vote
0
Answered by 6 years ago

This can be fairly tricky if you don't know how to randomize tables

function randomizeTable(tbl)
    for i=1,#tbl-1 do
        local rnd=math.random(i,#tbl)
        tbl[i],tbl[rnd]=tbl[rnd],tbl[i]
    end
    return tbl
end

That's a generic table shuffler, now what you're going to want to do is obtain a table of all players by doing...

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

Then randomize it

function randomizeTable(tbl)
    for i=1,#tbl-1 do
        local rnd=math.random(i,#tbl)
        tbl[i],tbl[rnd]=tbl[rnd],tbl[i]
    end
    return tbl
end

function killHalf()
    local Players = game:GetService("Players"):GetPlayers()
    local randomizedPlayers = randomizeTable(Players)
end

Then, just run a for loop that runs through half of the players

function randomizeTable(tbl)
    for i=1,#tbl-1 do
        local rnd=math.random(i,#tbl)
        tbl[i],tbl[rnd]=tbl[rnd],tbl[i]
    end
    return tbl
end

function killHalf()
    local Players = game:GetService("Players"):GetPlayers()
    local randomizedPlayers = randomizeTable(Players)
    for i=1,#randomizedPlayers/2 do
        local Character = randomizedPlayers[i].Character
        if Character then
            Character:BreakJoints()
        end
    end
end

Hope this helps!

0
How would i make it so it doesn't kill me everytime ATestAccount420 31 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
local killed = 0
for i,v in pairs(game.Players:GetPlayers()) do
    if killed < #game.Players:GetPlayers() then
        v.Character.Humanoid.Health = 0
    end
end
0
That will kill the same players every time, it needs to be randomized. M9_Sigma 35 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Hello there!

The generic for loop can only go through the whole list, not half of them.

We'll start by getting the number of times the killing should execute. In this case, the size of the list of player, divided by 2. We also need to make sure not to have decimals for the number of players, or things will get crazy! In this case, we'll use math.floor(num) to round it down (i.e. 5.6 --> 5; 3.99999 --> 3), but you can also use math.ceil(num), which rounds it up (i.e. 5.6 --> 6; 3.01 --> 4).

Step 1:

local players = game.Players:GetPlayers() -- Get the list of players. It would be used later.
for i = 1, math.floor(#players / 2) do -- The # means the number of elements in a table/list (array).

end

Since you want to kill players at random, we will use the Random object (unless you mean something else). We will construct the Random object with Random.new(), then use its :NextInteger(num1, num2) method to get the random player.

To learn more about the Random object, click here.

Step 2:

local players = game.Players:GetPlayers() -- Get the list of players. It would be used later.
local r = Random.new() -- You can also construct the Random object in the for loop, but I would rather put it here in case you want to generate other random numbers later on.
for i = 1, math.floor(#players / 2) do
    local randomPlayerIndex = r:NextInteger(1, #players) -- Get a number between 1 and the number of players.
end

To get the element based on the selected index, which is stored in randomPlayerIndex, we need to know about how arrays work.

Arrays (in this case, a list of players) have an index to reference in the code later on.

We do this to get the element in an array. local variableOfSomething = randomTable[index]

This is just an example, you don't need to put this in your actual code.

Now, back to your code, we will use randomPlayerIndex as the index when we get the player from the list. Then, we will get the player's character and set the humanoid's health in the character to 0 in order to kill the player!

Step 3:

local players = game.Players:GetPlayers() -- Get the list of players. It would be used later.
local r = Random.new() -- You can also construct the Random object in the for loop, but I would rather put it here in case you want to generate other random numbers later on.
for i = 1, math.floor(#players / 2) do
    local randomPlayerIndex = r:NextInteger(1, #players) -- Get a number between 1 and the number of players.
    local selectedPlayer = players[randomPlayerIndex] -- Get the player in the list of players that has this index.

    local char = selectedPlayer.Character -- Get the player's character.

    if char ~= nil then -- Just making sure the player's character still exists
        char:WaitForChild("Humanoid").Health = 0 -- Kill the player by setting the humanoid's health to 0. OOF!
    end
end

And that's it! I hope you'll like this solution. If you have any problems, please tell me in the comments. Thanks and have a great day!

0
I updated the code a little, should be okay now. starlebVerse 685 — 6y
0
How would i make it so it doesn't kill me everytime ATestAccount420 31 — 6y
0
one way is to shuffle the table to make it even more randomized. Use randomizeTable(players), which is in M9s's answer. (I hope you dont mind, M9s) starlebVerse 685 — 6y
0
whos answer do i accept cuz they both work.. ATestAccount420 31 — 6y
View all comments (2 more)
0
the choice is yours. starlebVerse 685 — 6y
0
btw sometimes this doesnt do half and less then that never more but sometimes less ATestAccount420 31 — 6y
Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Oh my freaking god dude:

for _,p in pairs(game.Players:GetPlayers()) do
    if p.Name ~= "ATestAccount420" then
        p.Character.Humanoid.Health = 0
    end
    if p.Name == "ATestAccount420" then return end -- Will NOT kill you
end

God, I don't even know if they know scripting.

0
However, one big disadvantage here: This kills EVERY player except you, not half of them. I'm sorry. DeceptiveCaster 3761 — 6y
0
i do know scripting and i already have it thx tho ATestAccount420 31 — 6y

Answer this question