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
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
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!
How would i make it so it doesn't kill me everytime
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
That will kill the same players every time, it needs to be randomized.
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!
I updated the code a little, should be okay now.
How would i make it so it doesn't kill me everytime
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)
whos answer do i accept cuz they both work..
the choice is yours.
btw sometimes this doesnt do half and less then that never more but sometimes less
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.
However, one big disadvantage here: This kills EVERY player except you, not half of them. I'm sorry.
i do know scripting and i already have it thx tho
This discussion is preserved as it was. New answers, comments, and votes are closed.
3 comments
if i <= #Players:GetPlayers()/2 then?ATestAccount420, the robloxian thanos
omg how did u know