How would I make an item go into a specific humanoid's backpack?

So basically I have a system when the round starts it picks a character for the player and inside the Humanoid, it puts a string value called CharName, and the value is the character’s name.

How would I make an item go to a specific character’s backpack when they spawn in?

I’m doing research but i’m still very confused

Use math.random().
Something like this

local players = game.Players:GetPlayers()
local SelectedPlayer = players[math.random(1, #players)]
--give the player the item
local item = --item here
item.Parent = SelectedPlayer.Backpack

You can do this:

local item = --item here

for _, p in pairs(game.Players:GetPlayers()) do
    if p.Character.Humanoid:FindFirstChild("CharName") ~= nil then
        local cloneitem = item:Clone()
        cloneitem.Parent = p.Backpack
    end
end

Basically, it loops through all the players in the server, then it checks if “CharName” is not nil (“nil” means that it does not exist or does not have any value). If so, then it clones the item to their Backpack

1 Like

So I would replace nil with the character’s name?

Well you said before that the selected player gets a StringValue named “CharName” put into their Humanoid, so I check if it exists in their humanoid before I clone the item to their backpack

1 Like

no but how would I specify in the script what the Character’s name in the string value is? The value becomes their name when the round starts. Only specific characters would get these special items.

Ohhhhh, just to clarify, do all the player’s have a string value in their Humanoid?

yes, but it’s added when the round starts

Ok, so you would do this:

--switched 'p' to 'player' to make it more clear
for _, player in pairs(game.Players:GetPlayers()) do
    if player.Character.Humanoid:FindFirstChild("CharName") ~= nil then
        player.Character.Humanoid.CharName.Value = player.Name
    end
end

so i combine the code like this?

Blockquotefor local item = game.ServerStorage.YouLikeThat --item here
local Fabio = game.Workspace.p
for _, player in pairs(game.Players:GetPlayers()) do
if player.Character.Humanoid:FindFirstChild(“CharName”) ~= nil then
player.Character.Humanoid.CharName.Value = player.Fabio
local cloneitem = item:Clone()
cloneitem.Parent = player.Backpack
end
end

Yep, that looks about right

--FULL CODE
for _, player in pairs(game.Players:GetPlayers()) do
    if player.Character.Humanoid:FindFirstChild("CharName") ~= nil then
        player.Character.Humanoid.CharName.Value = player.Name

        local cloneitem = item:Clone()
        cloneitem.Parent = player.Backpack
    end
end
2 Likes