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

Give a random player a weapon?

Asked by 6 years ago

I have a script that chooses a random player, but I don't understand how to give that random player a weapon. Any help would be appreciated :D

wait(10)
dft = {} 
local plrs = #game:GetService('Players'):GetChildren()

function GetPlayers()
    local c = game.Players:GetChildren() 
    for i = 1, #c do 
        table.insert(dft, c[i].Name) 
    end 
end

if plrs >= 2 then
function Randomize()
    GetPlayers()
    local d = math.random(1, #dft) 
    local s = d 
    local m = Instance.new("Message", game.Workspace) 
    m.Text = "The random player is ... "..dft[s].."!" 
    wait(2) 
    m:Remove()
    local rndm = game.Players:FindFirstChild(dft[s])
    if (rndm ~= nil) then 
        local Player = rndm.Character
        Player:MoveTo(Vector3.new(6, 13, 49))
    end
end

while wait(20) do    
    print("Done waiting.")
        print("Randomizing.")
        Randomize()
break
    end


else
    local h = Instance.new("Message")
h.Parent = workspace 
h.Text = "Not enough players."
wait(10)
h:Remove()
end
    plrs = #game:GetService('Players'):GetChildren()



0
Sorry but you're code isn't good, I already answered a way better code User#20388 0 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

I think you overthought this a little. All you have to do is select a random member of a table. I’ll leave an example below. You should also refresh the random player every time it is called, or it will give the same player each time. You can do that part yourself.

local Players = game.Players:GetPlayers()
local Random = math.random(1, #Players) 

The random variable is the same as your previous “Dft” variable. To give them a tool, just do something like this: (Clone the tool from elsewhere)

local Tool = game.ServerStorage:WaitForChild(“Tool”):Clone()
Tool.Parent = Players[Random].Backpack
0
how can i make it so it gives the tool to player 'dft'? coolduck12 4 — 6y
0
What is “dft”? ThatPreston 354 — 6y
0
the variable that defines the random player coolduck12 4 — 6y
0
I already have a variable for that in the script above. It’s called ‘Random’. ThatPreston 354 — 6y
Ad

Answer this question