Random player kill not working

I made this script to randomly kill a player, but it isn’t working and I have no idea why. got any ideas

Do math.random with #game.Players:GetChildren().

Is this happening in a server script or local? You can access Players on the server side, and randomly choose from the Players list who the victim would be.

local PlayersList = game:GetService("Players"):GetChildren()


print(PlayersList[math.random(1,#PlayersList)].Name)

beat me to it by a second hehe, what he said.

I am trying to only kill a random player within the Ingame folder which holds the players within the current round

Server script. I edited it a lil to look likre this and havent been able to test it out yet

are the players’ character model stored in a folder in workspace?

Assuming you put every character in the Ingame folder, you don’t need to put .Character when you set the health, only do that when you are getting it directly from the player like

local diedisease = math.random(1, #game.Players:GetPlayers())
diedisease.Character:FindFirstChild("Humanoid").Health = 0
1 Like

They are stored in the ingame folder only when in a round

This wouldn’t work as it would still get all the players instead of only ones in a round

Ah, I see, do

local diedisease = math.random(1, #game.Workspace.Ingame:GetChildren())
diedisease:FindFirstChild("Humanoid").Health = 0

Ok, i am trying this right now, thanks. I will tell you if it works

It didn’t work

OH, I see, math.random returns a number, you’ll have to do

local inGame = game.Workspace.Ingame:GetChildren()
local diedisease = math.random(1, #game.Workspace.Ingame:GetChildren())
local diedPlayer = inGame[diedisease]
diedPlayer:FindFirstChild("Humanoid").Health = 0

Tell me if it works.

1 Like

Do the brackets have to be there, they have a red error line under them? I think it wants them to be parenthesis

Yeah, they index the position of the player like if I had a table

{1, 2, 3, 4, 5}

The index number of 5 would be five because it is 5th in the table.

Can you show me what happens in studio?

You put the []s instead of {}, anyways I got to go, I will tell you if it works later

To make a table, you need to enclose em in curly brackets

{"hello", "world", "table"}

The index number will be enclosed in square brackets

table[indexnum]

You never put curly brackets though