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

Give a random player a weapon?

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



1 comment

0

Sorry but you're code isn't good, I already answered a way better code

1 answer

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

4 comments

0

how can i make it so it gives the tool to player 'dft'?

0

What is “dft”?

0

the variable that defines the random player

0

I already have a variable for that in the script above. It’s called ‘Random’.

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