#Random Player Picker Not Working

34 messages · Page 1 of 1 (latest)

regal cobalt
#

i scripted it so that when a player joins the game it picks a random player between 0 and the amount of player and then when the player is chosen the tool is gonna be inside the backpack of the chosen player it doesnt work its not putting the tool inside the backpack but it still prints the chosen player

here is the script:

local tool = game.ReplicatedStorage.Minigun

local Players = game.Players

local function pickRandomPlayer()
game.Players.PlayerAdded:Connect(function()
local chosen = Players:GetChildren()[math.random(0, #Players:GetChildren())]
if chosen ~= nil then
print(chosen)
tool.Parent = chosen.Backpack
else

    end

end)

end

pickRandomPlayer()
odd ferry
#

change the 0 to 1 in the math.random

regal cobalt
#

everything works but it doesnt put the tool in the backpack

safe sand
#

are there any errors in the output?

regal cobalt
#

nah

safe sand
#

you're checking if a player joins after using the function

#

why even make it a function

regal cobalt
#

should i call the function when the player joins?

regal cobalt
safe sand
#

move this ```game.Players.PlayerAdded:Connect(function()
local chosen = Players:GetChildren()[math.random(0, #Players:GetChildren())]
if chosen ~= nil then
print(chosen)
tool.Parent = chosen.Backpack
else

    end

end)```

outside the pickRabdomPlayer function

#

and delete this function, it's unnecessary

regal cobalt
#

alright

#

but it still doesnt work there is no difference

safe sand
#

post the updated script

regal cobalt
#

local tool = game.ReplicatedStorage.Minigun

local Players = game.Players

game.Players.PlayerAdded:Connect(function()
local chosen = Players:GetChildren()[math.random(1, #Players:GetChildren())]
if chosen ~= nil then
print(chosen)
tool.Parent = chosen.Backpack
else

end

end)

safe sand
#

try this: ```lua
local tool = game:GetService("ReplicatedStorage"):WaitForChild("Minigun")

local Players = game.Players

game.Players.PlayerAdded:Connect(function()
local chosen = Players:GetChildren()[math.random(1, #Players:GetChildren())]
if chosen ~= nil then
print(chosen)
tool.Parent = chosen.Backpack
else

end

end)```

regal cobalt
#

still doesnt work, the backpack is still empty

safe sand
#

i think there might be something wrong with the 8th line

regal cobalt
#

this?: local chosen = Players:GetChildren()[math.random(1, #Players:GetChildren())]

safe sand
#

yeah

regal cobalt
#

idk it still prints my name when it succesfully picks the random player which means there shouldnt be anything wrong with it

#

this is the main problem here:

tool.Parent = chosen.Backpack

safe sand
#

also you shouldn't change tool.Parent but duplicate that tool and set it's parent to backpack

regal cobalt
#

alright ill try that

#

still doesnt work 😭😭

safe sand
#

ok, i got it

#

you have to add it to the backpack when the character is added

regal cobalt
#

wait ima try

safe sand
#

so try using that

local tool = game:GetService("ReplicatedStorage"):WaitForChild("Minigun")


local Players = game.Players


game.Players.PlayerAdded:Connect(function()
    local chosen = Players:GetChildren()[math.random(1, #Players:GetChildren())]
    
    if chosen ~= nil then
        print(chosen)
        chosen.CharacterAdded:Connect(function()
            tool:Clone().Parent = chosen.Backpack
        end)
    else

    end
end)```
regal cobalt
#

IT WORKS

ivory thistle
# safe sand so try using that ```lua local tool = game:GetService("ReplicatedStorage"):WaitF...

or you can use like this
local Players = game.Players

game.Players.PlayerAdded:Connect(function()
local chosen = Players:GetChildren()[math.random(1, #Players:GetChildren())]
if chosen ~= nil and chosen.CharacterAdded:Wait() then
local tool = game:GetService("ReplicatedStorage"):WaitForChild("Minigun"):Clone()
tool.Parent = chosen:FindFirstChild("Backpack")
print(chosen)
end
end)

ivory thistle
regal cobalt
#

yeh i get it xd