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