When the player presses a button, I want an object from the server storage to be sent to the player’s backpack. On Roblox developer, they say that a server script can access the player’s backpack using
game.Players.LocalPlayer.PlayerName.Backpack, but it doesn’t work for me :(. Help! What should I do to be able to transfer a tool from ServerStorage to the player’s backpack?
You can use local player on server, and what you were told to use is really inefficient as well.
You check if a player presses a button on the server or client, ( server is better ) and then clone a tool from serverStorage and parent it to the player’s backpack. Also, it is better to check if the player already has the tool stored in their backpack or have currently equipped it ( character) and if they did, you can return true to prevent too many tools.
Can you please show me with a script how to do it?
Yes, I referenced game services, and only the player that touched the part will get a tool. Please fix the path since you haven’t told where the tool is, what’s the name etc.
--// Game Services
local ServerStorage = game:GetService("ServerStorage")
local Players = game:GetService("Players")
script.Parent.Touched:Connect(function(hit)
local player = Players:GetPlayerFromCharacter(hit.Parent)
if player then
local Tool = ServerStorage.Tool:Clone()
Tool.Parent = player.Backpack
end
end)
Lets say you’re button is a ClickDetector
ClickDetector.MouseClick:Connect(function(plr)
game.ServerStorage["Your Tool"]:Clone().Parent plr.Backpack
end)
Wait a local script can clone a tool from server storage?
Wait but why does it have local in it?
But do I have to put the server script inside the button?
Indeed, yes, you have to. ( 30 characters)
Bro you have seem to forgot, I have a GUI button, not an object you can touch
And (hit, parent) doesn’t seem to work there
MouseButton1Click event connected to a function would work for a GUI object
It doesn’t in my case (30 characters)
If you want to have a button that clones a tool from ServerStorage to the player’s backpack then you should follow this.
You have to have a remote event in Replicatedstorage that will fire once the button has been pressed.
The remote event should look something like this (You can name it whatever you want, mine gives a radio so it’s named Radio):
After you have the remote event you should have a localscript inside the button, it should look like this:
local Remote = game.ReplicatedStorage.RemoteEvents.Radio
local function onClick()
Remote:FireServer()
end
script.Parent.MouseButton1Click:Connect(onClick)
Then you have to have a server script inside ServerScriptService, it should look like this:
local tool = game.ServerStorage.Radio
local Remote = game.ReplicatedStorage.RemoteEvents.Radio
game.Players.PlayerAdded:Connect(function(player)
Remote.OnServerEvent:Connect(function()
tool:Clone()
tool.Parent = player.Backpack
end)
end)
This should work after you have changed the variables to match your objects.
I’d suggest adding a debounce to it though.
Are you giving the player the radio when the player joins?
The remote event fires when it the GUI object is clicked, hence the MouseButton1Click
Doesn’t work for me (30 characters)
Not good at all, exploiters can pass their own arguments.
Yeah I was thinking about the same thing, but I wrote it pretty fast and couldn’t get any better solutions in my head.