How would i make a gui visible for a specific player?

so im trying to make a trading system but im not sure how i can make a trade request gui appear for the player they want to send the trade to. How can i do this?

local targetName = script.Parent.Parent.playerName.Text
script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Text = "Sent"
	wait(3)
	script.Parent.Text = "Trade"
	
	for i, player in pairs(game:GetService("Players"):GetPlayers()) do
		if player.Name == targetName then
			script.Parent.Parent:WaitForChild("TradeRequest").Visible = true --Idk what to do at this part
		end
	end
end)
1 Like

By using remote events, you can send the target player’s info, receive it server-side and relay data to the target player, where a script handles the trading UI from that side.

Im not really sure how to do this part though

On the server script, have a part that receives a remote event from the player. Then, fire an event at the target player and send the data you want.

would i do it like this?

Remotes.ConfirmTrade.OnServerEvent:Connect(function(player, targetName)
	for i, trader in pairs(game:GetService("Players"):GetPlayers()) do
		if trader.Name == targetName then
			Remotes.ConfirmTrade:FireClient(player.Name)
		end
	end
end)

The first argument in onServerEvent is the player who fired the remote. In your case, this would send a confirmation signal to the same player that sent the request. Plus, you can get the player object in an easier and more efficient way by using this:

local plr = game:GetService("Players")
local trader = plr:FindFirstChild(targetName)

which would return the player object of a player by their name.

for the last part when im sending it back to the client do i send it with the player.Name parameter? Because like i want text to say like “player1 Sent you a trade request”

To send it to a specific client, you need to use a player object and not a string. You can pass the name of the person who sent the request as a second argument to the remote.

sorry, but im kind of confused. So would i send 3 parameters to the client? player, trader, player.Name?

Yes, although the player object as the first argument doesn’t get passed, it’s only for the server to know who to send the data to.

would it be like this?

Remotes.ConfirmTrade.OnServerEvent:Connect(function(player, targetName)
	local plr = game:GetService("Players")
	local trader = plr:FindFirstChild(targetName)
	
	if trader then
		Remotes.ConfirmTrade:FireClient(player, trader, player.Name)
	end
end)
Remotes.ConfirmTrade.OnClientEvent:Connect(function(trader, name)
	script.Parent.Parent:WaitForChild("TradeRequest").Visible = true
	script.Parent.Parent:WaitForChild("TradeRequest").RequestInfo.Text = name.." has sent you a trade request"
end)

While you’re correctly using the remote system, you’re also sending the request to the same player that sent it. Try setting the first argument as trader and the second argument as player.Name, then remove all other arguments that aren’t used.

if trader then
		Remotes.ConfirmTrade:FireClient(trader, player.Name)
end
Remotes.ConfirmTrade.OnClientEvent:Connect(function(name)
	script.Parent.Parent:WaitForChild("TradeRequest").Visible = true
	script.Parent.Parent:WaitForChild("TradeRequest").RequestInfo.Text = name.." has sent you a trade request"
end)

hmm okay. Would this be all that i would have to do or is there more?

If you don’t need help with anything else, that’s all you really need.

The gui isn’t turning on. I think it’s because of the targetname variable. How would i send it to the targeted player? Without using the

 local targetName = script.Parent.Parent.playerName.Text

because i don’t think this variable would work since it’s getting the player’s name that is sending it