Search code examples
luaclient-servergame-engineroblox

How to give people tools using a remote event?


I have a local script that says when a part is clicked it will send the name of the player to the serverside for handing out the weapon. When the the event is fired it goes to this code in the server script side

ReplicatedStorage.RemoteEvent:OnServerEvent:Connect(function(playerWhoWantsTool)
     local toolClone = tool:Clone()
     print(playerWhoWantsTool)-- It prints out my username in the output so I know it works
     local buyersBackPack = game.Players.playerWhowantsTool.Backpack
     toolClone.Parent = buyersBackPack
     -- Returns an error about cant find playerwhowantsTool in game.Players
end)

I'm not sure how to go about giving a player a tool with a server script,Thanks.

I was expecting for the identified player to receive the tool in their backpack.


Solution

  • But when you call a RemoteEvent's FireServer function, the OnServerEvent event will provide the player that fired the event as the first argument. That variable is a Player object, so you have access to the Backpack right from that object. You don't need to index into the Players service to find the player again.

    ReplicatedStorage.RemoteEvent:OnServerEvent:Connect(function(player)
         local toolClone = tool:Clone()
         toolClone.Parent = player.Backpack
    end)