Just get a random player using game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())] --wil return a random player and them copy the AR1 and parent put its parent as the player.
local function GiveRandomPlayerAR()
local randomPlayer = game.Players:GetChildren()[math.random(1,#game.Players:GetChildren())]
local Model = game.GetStorage["AR-15"]:Clone()
Model.Parent = randomPlayer
end
GiveRandomPlayerAR() -- calls the function
You’re probably firing it before the player joins the server. You could try adding an task.wait() before (I wrote something wrong too, the code below is working).
task.wait(2) -- wait for player to join
local function GiveRandomPlayerAR()
local PlayerList = game.Players:GetChildren()
local randomPlayer = PlayerList[math.random(1,#PlayerList)]
local Model = game.ServerStorage["AR-15"]:Clone()
Model.Parent = randomPlayer
print(randomPlayer.Name.." was chosen!")
end
GiveRandomPlayerAR() -- calls the function
local function GiveRandomPlayerAR()
local PlayerList = game.Players:GetChildren()
if #PlayerList > 0 then
local randomPlayer = PlayerList[math.random(1, #PlayerList)]
local Model = game.ServerStorage["AR-15"]:Clone()
Model.Parent = randomPlayer.Backpack
print(randomPlayer.Name .. " was chosen!")
else
warn("No players in the game to give the AR-15 to.")
end
end
-- call the function when a player joins
game.Players.PlayerAdded:Connect(function()
task.wait(2) -- wait for player to fully join
GiveRandomPlayerAR()
end)
local hasGivenAR = false
local function GiveRandomPlayerAR()
if hasGivenAR then
return
end
local PlayerList = game.Players:GetChildren()
if #PlayerList > 0 then
local randomPlayer = PlayerList[math.random(1, #PlayerList)]
local Model = game.ServerStorage["AR-15"]:Clone()
Model.Parent = randomPlayer.Backpack
print(randomPlayer.Name .. " was chosen and given an AR-15!")
hasGivenAR = true
else
warn("No players in the game to give the AR-15 to.")
end
end
-- Call the function when a player joins
game.Players.PlayerAdded:Connect(function()
task.wait(2) -- wait for player to fully join
GiveRandomPlayerAR()
end)
-- Initial call in case players are already in the game
task.wait(2) -- wait for initial players to join
GiveRandomPlayerAR()