Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to find a player by using Remote Function?

LocalScript:

Calls Remote Function by pressing the Button

local Player = game.Players.LocalPlayer
local button = script.Parent

button.MouseButton1Down:connect(function(player)
				game.ReplicatedStorage.RemoteFunction:InvokeServer(player)
		end
	  end)

Server Script:

Gives template image to a Player

local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")

function ReplicatedStorage.RemoteFunction.OnServerInvoke(player)
-- Works until this moment ---------------------------------------------------
	local Inv = player.PlayerGui:WaitForChild("InventoryGui")
	local NewSlot = player.PlayerGui:WaitForChild("InventoryGui").Template:Clone()
	NewSlot.Parent = player.PlayerGui:WaitForChild("InventoryGui").Folder
	return NewSlot
end

This is still working in Studio, but not on the server.

1 comment

0

Remote events and functions will add the player as the first argument for you as Roblox knows who sent the request. You do not need to add the player when calling the remote event or function.

1 answer

✓ Accepted answer

The first argument is the player that sent it, so you should first have the player sending it then the player you want to have the gui visible to. Also, with Filtering Enabled, I don't believe you can see a PlayerGui in server side. You may want to use RemoteEvents in this case, and I will show you the code you should use.

First, create a remote event and name it. In this case, I will name it giveItem.

LocalScript inside button:

local Player = game.Players.LocalPlayer
local button = script.Parent

button.MouseButton1Down:connect(function()
	if game.Players:FindFirstChild(button.Text) then
         game.ReplicatedStorage.giveItem:FireServer(game.Players:FindFirstChild(button.Text))
	end
end)

This will find the player and send it to the server.

Server Script:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

ReplicatedStorage.giveItem.OnServerEvent:connect(function(player, sentPlayer)
	ReplicatedStorage.giveItem:FireClient(sentPlayer) -- Tell the client to clone the Template.
end)

This will receive the request and tell the player being requested that the GUI needs cloned and moved.

LocalScript in InventoryGUI:

game.ReplicatedStorage.giveItem.OnClientEvent:connect(function()
	local temp = script.Parent.Template:Clone()
	temp.Parent = script.Parent.Folder
end)

This will receive the server request to clone the Template and put it in the Folder, you may add arguments too for editing the template.

If you need further help, please comment below so I can assist you!

EDIT:

If you wanted to use one script, do the following:

In the localscript with OnClientEvent, replace it with this code, and delete the code in the buttons.

local buttons = script.Parent.Buttons --Change to where buttons are stored.

game.ReplicatedStorage.giveItem.OnClientEvent:connect(function()
	local temp = script.Parent.Template:Clone()
	temp.Parent = script.Parent.Folder
end)

for i,button in pairs (buttons:GetChildren()) do
	if button:IsA('TextButton') or button:IsA('ImageButton') then
		button.MouseButton1Down:connect(function()
			if game.Players:FindFirstChild(button.Text) then
		  		game.ReplicatedStorage.giveItem:FireServer(game.Players:FindFirstChild(button.Text))
			end	
		end)
	end
end

5 comments

0

Is there any way to send some information or even find a player through Remote Function? So I could ask Remote Function to clone my Gui from the LocalScript that located in Button.

0

Why a remotefunction? Your button isn't using this data.

0

So I need two different LocalScripts in my Gui? Can I avoid this and use only one?

0

Yes, I will make an edit on that.

0

Oh.. And how can I send text and text color to my Event? (function(Here?) or FireServer(Here?),or FireClient(Here?)

This discussion is preserved as it was. New answers, comments, and votes are closed.