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

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

Asked by 2 months ago

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?

1
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 1337 — 2mo
0
Can you explain in an answer? barrettr500 26 — 2mo

1 answer

Log in to vote
2
Answered by
xPolarium 1337 Moderation Voter
2 months ago
Edited 2 months ago

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

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:

0
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 26 — 2mo
Ad

Answer this question