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

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

Asked by 3 years ago
Edited 3 years ago

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.

0
Try learning math.random. Also learn how to put your code in a code block 3wdo 198 — 3y
0
I put it in a code block for you CatNinja4893 -3 — 3y

2 answers

Log in to vote
0
Answered by
Oxprem 140
3 years ago

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
0
Okay, I'll test it once I finish school CatNinja4893 -3 — 3y
0
ok, well that didn't seem to work. CatNinja4893 -3 — 3y
0
the error in the output said "ServerScriptService.Script:7: attempt to index nil with 'Name'" CatNinja4893 -3 — 3y
0
Hmmm... well, in that case, try removing .Name on lines 7 and 8 Oxprem 140 — 3y
View all comments (6 more)
0
ok CatNinja4893 -3 — 3y
0
did it work? Oxprem 140 — 3y
0
nope :/ CatNinja4893 -3 — 3y
0
Should it be a Local Script? CatNinja4893 -3 — 3y
0
I figured it out CatNinja4893 -3 — 3y
0
Thats good to hear! also, sorry for not replying. Had school. Oxprem 140 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

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")

0
Yeah, that didn't work either. CatNinja4893 -3 — 3y

Answer this question