##RemoteEvents
####RemoteEvents are used to communicate between client to server, and vice-versa, to tell each other when to run some code.
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.
######We follow the client-to-server communication route so we can start in the local script:
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local player = Players.LocalPlayer
local remoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
script.Parent.MouseButton1Click:connect(function()
local desiredName = script.Parent.Parent.Username.Text
remoteEvent:FireServer(desiredName)
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.
######In the server script is where we handle finding the desired player and moving the children:
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.
####References:
2 comments
You'll need to use a RemoteEvent to have the server move the children of the LocalPlayer's backpack to the other player's backpack.
Can you explain in an answer?