Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I make a script that randomly chooses a player and kills them?

How do I make a random player chooser? All I want it to do for now is kill the player. I thought I could do this:

local RP = game.Players:FindFirstChild(Random)

RP.Humanoid.Health = 0

But that didn't work. I also tried:

local RP = game.Players:FindFirstChild()

RP.Humanoid.Health = 0

and:

local RP = game.Players:FindFirstChild(function()

RP.Humanoid.Health = 0

But none of those worked. PLEASE please PLEASE help me!

FYI, I put this script in ServerScriptService.

2 comments

0

Try learning math.random. Also learn how to put your code in a code block

0

I put it in a code block for you

2 answers

Perhaps this might work

local Players = game.Players:GetPlayers()
local nplrs = #Players
local Randomplayer = nil
if nplrs > 0 then
     Randomplayer = Players[math.random(1, nplrs)]
end
if workspace[RandomPlayer.Name]:FindFirstChild("Humanoid") then
	workspace[RandomPlayer.Name]:FindFirstChild("Humanoid").Health = 0
end

10 comments

0

Okay, I'll test it once I finish school

0

ok, well that didn't seem to work.

0

the error in the output said "ServerScriptService.Script:7: attempt to index nil with 'Name'"

0

Hmmm... well, in that case, try removing .Name on lines 7 and 8

0

ok

0

did it work?

0

nope :/

0

Should it be a Local Script?

0

I figured it out

0

Thats good to hear! also, sorry for not replying. Had school.

i know your question is already answered, but i thought for practice i might give it a shot, hope this works

local plrs = game.Players:GetPlayers()
local rando = plrs[math.random(1,#plrs)]

if rando.Character then
	if rando.Character:FindFirstChild("Humanoid") then
		rando.Character.Humanoid.Health = 0
	end
end
	
print(rando.Name.." was chosen to die")

1 comment

0

Yeah, that didn't work either.

This discussion is preserved as it was. New answers, comments, and votes are closed.