In this hand item to another player script
local name = game.Players.LocalPlayer script.Parent.MouseButton1Click:connect(function() local wanted = game.Players:FindFirstChild(script.Parent.Parent.Username.Text) for i, v in pairs (name.Backpack:GetChildren()) do if wanted then v.Parent = wanted.Backpack else print("NoPlayerFound") end end end)
it works, but its a local script... so it only goes to the other player according to the first players screen, how would I actually make it go to them, or is it possible to make a clickable gui script not local?
In your local script you're having the player click on a GuiButton which is considered done on the client. You then try moving children from the LocalPlayer's Backpack to another player's Backpack. The issue with this is that it won't replicate for the server and other players. For it to replicate, we need the server to handle the re-Parenting.
Before you start you will need a RemoteEvent object in ReplicatedStorage, a server Script to handle the OnServerEvent
code, and the same local script you have.
--define services we will use local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local player = Players.LocalPlayer --Give reasonable variable names local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent") --Wait for our remote script.Parent.MouseButton1Click:connect(function() local desiredName = script.Parent.Parent.Username.Text --Get the text to send remoteEvent:FireServer(desiredName) --Fire the remote with one argument end)
The event in the above code uses only two lines to communicate to the server. Why?
It's because the server will and should handle the important parts for replicating our changes. All we send is the Text in the GuiObject called Username to the server since the server wouldn't have access to this.
local Players = game:GetService("Players") local ReplicatedStorage = game:GetService("ReplicatedStorage") local remoteEvent = ReplicatedStorage.RemoteEvent remoteEvent.OnServerEvent:Connect(function(player, desiredName) local desiredPlayer = Players:FindFirstChild(desiredName) if desiredPlayer then for _, tool in pairs(player.Backpack:GetChildren()) do tool.Parent = desiredPlayer.Backpack end end end)
We connect the OnServerEvent
which should provide us with two parameters. The first is the player
that communicated to the server that had clicked the GuiButton. The other is a string type we call desiredName
and is the same argument we passed in the local script code.
If you're confused as to why there are two parameters in this specific event then review the article I had linked at the top of this answer.
Next, we get the player instance in the Players service that is named desiredName
. If they exist then we get the children in the original player's Backpack and re-parent them to the desiredPlayer
. That wraps up the client-to-server communication.
There is a lot to take in since I'm assuming you have no knowledge of RemoteEvents. I really recommend you take a look at the references I've linked since it helps knowing how replication works in Roblox.
If I missed anything or you need something explained let me know.