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

Copy an item into a certain players backpack?

Asked by 6 years ago
Edited 6 years ago

Hello,

I have a variable with a players username, how do I place the item in their backpack?

https://gyazo.com/9ead9101f8a54cf44ff75a0b7fc735f7

while true do
    wait(0.01)
    reciever = script.Parent.UserBox.Username.Value
    game.Lighting["Card"]:Clone().Parent = game.Players.reciever.Backpack
    script.Parent.Parent.Username.Value = ""
end

Thanks, Ethan!

0
don't use a looop BlackOrange3343 2676 — 6y
0
Is that was is making it not work? Starhawke_r 11 — 6y

2 answers

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

Its best to use a local script for this Put the local script in playergui or starterpack or starterplayer

    local reciever = "RecieverName" -- Put it to the reciever name
    local player = game.Players.LocalPlayer
    game.Players.PlayerAdded:Connect(function() -- Whenever the player joins the server
        if reciever and player and player.Name == reciever then
            game.Lighting.Card:Clone().Parent = player.Backpack
        end
    end)

If this answer worked then Please submit this answer

0
What I mean is, the staff put in someones username, and then the player who's username they put in recieves the item. Starhawke_r 11 — 6y
Ad
Log in to vote
0
Answered by
Azarth 3141 Moderation Voter Community Moderator
6 years ago
Edited 6 years ago

From your comment on sal, I have configured it for you a different way. This way, you have a textbox and every time you enter text it looks for that player and awards them the card if they can be found and they don't already have it. If it's not exactly what you're envisioning, I'm sure you can configure it back with the errors that have been explained. Please take a look at this screenshot to see how your hierarchy should look.

-- LocalScript

local ReplicatedStorage = game:GetService('ReplicatedStorage')
local sp = script.Parent
-- Use WaitForChild() on children
local reciever = sp:WaitForChild('TextBox')
-- I'm assuming a Username Value is inside the player?
-- Use ReplicatedStorage if LocalScripts need to access something
-- Use ServerStorage only if scripts need to access something
local card = ReplicatedStorage:WaitForChild("Card")


local function player_hascard(player)
    -- Make sure they don't already have a card
    local inbackpack, equipped = player.Backpack:findFirstChild("Card"), player.Character:findFirstChild("Card")
    if inbackpack or equipped then 
        return true
    end
    return false
end

local function return_player(player_name)
    -- Make sure they exist
    -- GetPlayers only returns objects that have the 
        -- ClassName Player
    -- Use GetPlayers() not GetChildren()
    local found = false
    for i,v in pairs(game.Players:GetPlayers()) do 
        -- if player_name == the name we just looped through
            -- Then we found the player from the textbox text
        if player_name == v.Name:lower() then 
            -- if that player doesn't already have a card
                -- make found = that player and break the loop
                    -- and return that player
            if not player_hascard(v) then 
                found = v
                break
            end
        end
    end
    return found
end

-- Use the changed event to detect change in something
-- While loops cause lag
-- Use 'C'onnect, 'c'onnect is deprecated (not supported)
reciever.Changed:Connect(function()
    local find_player = return_player(reciever.Text:lower())
    if find_player then
        card:Clone().Parent = find_player.Backpack
    end
end)
0
Thanks will try it out later when I am free. Starhawke_r 11 — 6y

Answer this question