How do I make it so when a random number is selected it kills the player

Hello I am trying to make it so the system cycles through numbers 1 to 20 and when one of the numbers is selected, it kills the player

The issue is that I am struggling to find the local player in a server script and don’t know how to get that

I tried using youtube and other topics on the forum to see how to do it but didn’t find anything useful

this is the code I currently have

local Players = game:GetService("Players")
random = math.random(1,20)

while true do
	print(math.random(1,20))
	wait(2)
	if random == 10 or 20 or 15 or 12 then
		game.Players.PlayerAdded:Connect(function(player)
			player.Parent:FindFirstChild("Humanoid")
			player.Humanoid:TakeDamage(player.Humanoid.MaxHealth)
	end)
	end	
	end

1 Like

who is THE player?
are you talking about the 20th player in the game.Players?
A specific player?
you cant run localplayer in a server script, as there is no local player, its the server, not a specific player

You could make a table of all the players and just get the specific player there, you could specify who it is why sending a remote event from a client script, or you could add a IntValue to players and number them with that, but it all depends on what you are trying to achieve

1 Like

oh i am just testing it on myself for practicing purposes but in general it would probably be like a random player i guess

just do game:GetService(“Players”):GetChildren()[random]

Totally didn’t just search up “how to get nth child roblox” on google lol

idk if this would work tbh since I cant test with studios

The proper code would be similar to what you replied with:

local players = game:GetService("Players"):GetPlayers()
local selectedPlayer = players[math.random(#players)]
-- This is essentially indexing the 'players' table with a random number between 1 and the total length of the table
-- so for example, this is doing 'players[20]' or 'players[3]'
-- the total length of the table depends on how many players are in the game
1 Like

this is giving me an error saying that the interval is empty

Its because the math.random doesnt know the maxmium value for it to choose randomly, so do

local selectedPlayer = players[math.random(1, #players)]

That means that when the script initally runs, no players are in the game. For something like this to work, you would make sure atleast one person is in the game (or however many you would recommend)


If one value is provided to math.random, it automatically does math.random(1, n)

i put the code you recommended in then tried to test it by printing a random players name when in game to see if it works, and it came back with that error despite me being in the game myself

Is it being run in a server script or a local script? If its a server script, the script runs before your player is connected to the game

yes its a server script, how would i fix this then?

the easiest thing (not exactly the best) is to just put a wait (someone said task.wait is always better but idk just use either) infront of the script

okay yeah that works thank you

You would want to wait for a player to connect the game and do the check from there:

-- Example
local players = game:GetService("Players")
local minPlayers = 4 -- the minimum amount of players needed to execute the code correctly

local function onPlayerAdded(player: Player)
   local playersArray = players:GetPlayers()
   if #playersArray < minPlayers then
      -- check if the minimum amount of players are in the game
      -- if there are less than the minimum amount, stop the function
      return
   end

   local selectedPlayer = playersArray[math.random(#playersArray)]
   --... do something to the selected player
end

players.PlayerAdded:Connect(onPlayerAdded) -- listen to when a player joins the game

Or you can do what @PANDASonNOOB suggested

1 Like

To save some lines of code you can just do players.PlayerAdded:Wait()

1 Like

im not making like a whole game yet im just sort of practicing and diversifying my knowledge so dont need this depth of script, i did what panda suggested and now it selects a player, how then would i kill them as my code comes back with the error of humanoid not being a valid member of my character

You’d want to use WaitForChild:

-- Example
local humanoid = Character:WaitForChild("Humanoid")
1 Like

ive got this so far however the character still doesnt die, though the error from before doesn’t show up

wait(3)
local players = game:GetService("Players"):GetPlayers()
local selectedPlayer = players[math.random(1, #players)]



print(selectedPlayer.Name)
random = (math.random(1,20))
print(math.random(1,20))
wait(2)
if random == 10 or 20 or 15 or 12 then
	local humanoid = selectedPlayer:WaitForChild("Humanoid")
	selectedPlayer.Parent:FindFirstChild("Humanoid")
	selectedPlayer.Humanoid:TakeDamage(selectedPlayer.Humanoid.MaxHealth)
end

you want to use selectedPlayer.Character to get the character i think

I see the issue:

This returns a Player Instance, not a character. To get their character, you have to do

local character = selectedPlayer.Character or selectedPlayer.CharacterAdded:Wait()
-- this gets the player's character if it loaded, or waits for it to load if it didn't

Then you would replace selectedPlayer with character here since the character is what you actually want to get:

local humanoid = selectedPlayer:WaitForChild("Humanoid")
selectedPlayer.Parent:FindFirstChild("Humanoid")
selectedPlayer.Humanoid:TakeDamage(selectedPlayer.Humanoid.MaxHealth)

This also won’t work the way you think; this will evalute to

if n == 10 or truthy or truthy or truthy or truthy then

or doesn’t check the to see if the any of the values are the result of random, it will see the truthy values and pass the check every time: In simple words, the check will always evalute to true because anything other than nil or false are seen as truthy values.

Your check should be like this:

if table.find({10, 20, 15, 12}, random) then

This checks if the value of random is in the array provided to table.find.