Still have questions? Join our Discord server and get real time help.
Log in to vote

How would I make a local script go to all players?

Asked by 6 months ago

In this hand item to another player script

local name = game.Players.LocalPlayer
    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

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?

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. xPolarium 1358 — 6mo
Can you explain in an answer? barrettr500 33 — 6mo

1 answer

Log in to vote
Answered by
xPolarium 1358 Moderation Voter
6 months ago
Edited 6 months ago


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:
--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

    local desiredName = script.Parent.Parent.Username.Text --Get the text to send
    remoteEvent:FireServer(desiredName) --Fire the remote with one argument

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

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.


Thanks alot, you went above and way beyond to explain this to me lol. This was really help full and I'll definitely look at those. barrettr500 33 — 6mo

Answer this question