HowToRoblox

TradeClient

Nov 20th, 2022
754
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.42 KB | None | 0 0
  1. --VARIABLES
  2. local client = game.Players.LocalPlayer
  3.  
  4. local rs = game.ReplicatedStorage:WaitForChild("TradeReplicatedStorage")
  5. local re = rs:WaitForChild("RemoteEvent")
  6. local config = require(rs:WaitForChild("CONFIGURATION"))
  7.  
  8. local tradeRequestsFolder = rs:WaitForChild("TRADE REQUESTS")
  9. local ongoingTradesFolder = rs:WaitForChild("ONGOING TRADES")
  10.  
  11.  
  12. local gui = script.Parent
  13.  
  14. local openBtn = gui:WaitForChild("OpenSendTrades")
  15. local sendTradesFrame = gui:WaitForChild("SendTradesFrame")
  16. local tradeRequestFrame = gui:WaitForChild("TradeRequestFrame")
  17. local tradeFrame = gui:WaitForChild("TradeFrame")
  18.  
  19. sendTradesFrame.Visible = false
  20. tradeRequestFrame.Visible = false
  21. tradeFrame.Visible = false
  22.  
  23.  
  24. --TRADE REQUESTS
  25. tradeRequestsFolder.ChildAdded:Connect(function(child)
  26.  
  27.     if child.Value == client.Name then
  28.         tradeRequestFrame.TradeText.Text = child.Name .. " sent you a trade request!"
  29.        
  30.         tradeRequestFrame.AcceptButton.Visible = true
  31.         tradeRequestFrame.RejectButton.Visible = true
  32.         tradeRequestFrame.Visible = true
  33.        
  34.     elseif child.Name == client.Name then
  35.         tradeRequestFrame.TradeText.Text = "You sent a trade request to " .. child.Value
  36.        
  37.         tradeRequestFrame.AcceptButton.Visible = false
  38.         tradeRequestFrame.RejectButton.Visible = true
  39.         tradeRequestFrame.Visible = true
  40.     end
  41. end)
  42.  
  43. tradeRequestsFolder.ChildRemoved:Connect(function(child)
  44.    
  45.     if child.Value == client.Name or child.Name == client.Name then
  46.         tradeRequestFrame.Visible = false
  47.     end
  48. end)
  49.  
  50.  
  51. --ONGOING TRADES
  52. ongoingTradesFolder.ChildAdded:Connect(function(child)
  53.    
  54.     if child:WaitForChild("Sender").Value == client.Name or child:WaitForChild("Receiver").Value == client.Name then
  55.        
  56.         local clientValue = child:WaitForChild("Sender").Value == client.Name and child.Sender or child.Receiver
  57.         local otherPlrValue = clientValue.Name == "Sender" and child.Receiver or child.Sender
  58.        
  59.         clientValue.AncestryChanged:Connect(function()
  60.             if clientValue.Parent == nil then
  61.                 tradeFrame.Visible = false
  62.                 openBtn.Visible = true
  63.             end
  64.         end)
  65.        
  66.         tradeRequestFrame.Visible = false
  67.         sendTradesFrame.Visible = false
  68.         openBtn.Visible = false
  69.        
  70.         tradeFrame.TradingFrame.TradingWithName.Text = "Trading with " .. otherPlrValue.Value
  71.         tradeFrame.TradingFrame.TheirOfferFrame.TheirOfferText.Text = otherPlrValue.Value .. "'s offer"
  72.         tradeFrame.TradingFrame.PlayerAccepted.Text = ""
  73.        
  74.         tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 = Color3.fromRGB(58, 191, 232)
  75.        
  76.         for i, child in pairs(tradeFrame.TradingFrame.YourOfferFrame:GetChildren()) do
  77.             if child:IsA("TextButton") or child:IsA("ImageButton") or child:IsA("Frame") then
  78.                 child:Destroy()
  79.             end
  80.         end
  81.         for i, child in pairs(tradeFrame.TradingFrame.TheirOfferFrame:GetChildren()) do
  82.             if child:IsA("TextButton") or child:IsA("ImageButton") or child:IsA("Frame") then
  83.                 child:Destroy()
  84.             end
  85.         end
  86.        
  87.        
  88.         --Alert client when other player has accepted
  89.         otherPlrValue.ChildAdded:Connect(function(child)
  90.             if child.Name == "ACCEPTED" then
  91.                 tradeFrame.TradingFrame.PlayerAccepted.Text = otherPlrValue.Value .. " has accepted"
  92.             end
  93.         end)
  94.         otherPlrValue.ChildRemoved:Connect(function(child)
  95.             if child.Name == "ACCEPTED" then
  96.                 tradeFrame.TradingFrame.PlayerAccepted.Text = ""
  97.             end
  98.         end)
  99.        
  100.        
  101.         --Display player's inventory
  102.         local inventoryList = tradeFrame.InventoryFrame.InventoryList
  103.         for i, child in pairs(inventoryList:GetChildren()) do
  104.             if child:IsA("TextButton") or child:IsA("ImageButton") then
  105.                 child:Destroy()
  106.             end
  107.         end
  108.        
  109.         local clientTools = config.GetTools(client)
  110.        
  111.         for i, tool in pairs(clientTools) do
  112.             local newToolButton = script:WaitForChild("ItemButton"):Clone()
  113.             newToolButton.Name = tool["TRADING ID"].Value
  114.            
  115.             newToolButton.ItemName.Text = tool.Name
  116.             newToolButton.IsInTrade.Visible = false
  117.            
  118.             if string.len(tool.TextureId) > 0 then
  119.                 newToolButton.ItemViewportFrame.Visible = false
  120.                 newToolButton.ItemImageLabel.Image = tool.TextureId
  121.                
  122.             else
  123.                 newToolButton.ItemImageLabel.Visible = false
  124.                
  125.                 local toolModel = Instance.new("Model")
  126.                 for x, descendant in pairs(tool:GetDescendants()) do
  127.                     if descendant:IsA("BasePart") then
  128.                         descendant:Clone().Parent = toolModel
  129.                     end
  130.                 end
  131.                
  132.                 toolModel.PrimaryPart = toolModel:FindFirstChild("Handle") or toolModel:FindFirstChildOfClass("BasePart", true)
  133.                 toolModel:SetPrimaryPartCFrame(CFrame.new(0, 0, -3))
  134.  
  135.                 toolModel.Parent = newToolButton.ItemViewportFrame
  136.  
  137.                 local vpfCamera = Instance.new("Camera")
  138.                 vpfCamera.CFrame = CFrame.new()
  139.                 vpfCamera.Parent = newToolButton.ItemViewportFrame
  140.             end
  141.            
  142.  
  143.             newToolButton.MouseButton1Click:Connect(function()
  144.                 if not tradeFrame.TradingFrame.YourOfferFrame.Slots:FindFirstChild(tool["TRADING ID"].Value) then
  145.                    
  146.                     newToolButton.IsInTrade.Visible = true     
  147.                     re:FireServer("add item to trade", {tool})
  148.                    
  149.                 else
  150.                     newToolButton.IsInTrade.Visible = false
  151.                     re:FireServer("remove item from trade", {tool})
  152.                 end
  153.             end)
  154.            
  155.             newToolButton.Parent = inventoryList
  156.         end
  157.        
  158.         --Display client's offer
  159.         local clientOffer = child[clientValue.Value .. "'s offer"]
  160.        
  161.         clientOffer.ChildAdded:Connect(function(child)
  162.            
  163.             local newToolButton = inventoryList[child:WaitForChild("TRADING ID").Value]:Clone()
  164.             newToolButton.IsInTrade.Visible = false
  165.             newToolButton.Size = script.ItemButton.Size
  166.            
  167.             newToolButton.MouseButton1Click:Connect(function()
  168.                 local plrTools = config.GetTools(client)
  169.                 for i, plrTool in pairs(plrTools) do
  170.                    
  171.                     if child["TRADING ID"].Value == plrTool["TRADING ID"].Value then
  172.                         re:FireServer("remove item from trade", {plrTool})
  173.                         break
  174.                     end
  175.                 end            
  176.             end)
  177.            
  178.             child.AncestryChanged:Connect(function()
  179.                 if child.Parent == nil then
  180.                     tradeFrame.InventoryFrame.InventoryList[newToolButton.Name].IsInTrade.Visible = false
  181.                     newToolButton:Destroy()
  182.                 end
  183.             end)
  184.            
  185.             newToolButton.Parent = tradeFrame.TradingFrame.YourOfferFrame.Slots
  186.         end)
  187.  
  188.        
  189.         --Display other player's offer
  190.         local otherPlrOffer = child[otherPlrValue.Value .. "'s offer"]
  191.        
  192.         otherPlrOffer.ChildAdded:Connect(function(child)
  193.            
  194.             local newToolButton = script.ItemButton:Clone()
  195.             newToolButton.Name = child:WaitForChild("TRADING ID").Value
  196.            
  197.             newToolButton.ItemName.Text = child.Name
  198.             newToolButton.IsInTrade.Visible = false
  199.             newToolButton.AutoButtonColor = false
  200.  
  201.             if string.len(child.TextureId) > 0 then
  202.                 newToolButton.ItemViewportFrame.Visible = false
  203.                 newToolButton.ItemImageLabel.Image = child.TextureId
  204.  
  205.             else
  206.                 newToolButton.ItemImageLabel.Visible = false
  207.  
  208.                 local toolModel = Instance.new("Model")
  209.                 for x, descendant in pairs(child:GetDescendants()) do
  210.                     if descendant:IsA("BasePart") then
  211.                         descendant:Clone().Parent = toolModel
  212.                     end
  213.                 end
  214.                 toolModel.PrimaryPart = toolModel:FindFirstChild("Handle") or toolModel:FindFirstChildOfClass("BasePart", true)
  215.                 toolModel:SetPrimaryPartCFrame(CFrame.new(0, 0, -3))
  216.  
  217.                 toolModel.Parent = newToolButton.ItemViewportFrame
  218.  
  219.                 local vpfCamera = Instance.new("Camera")
  220.                 vpfCamera.CFrame = CFrame.new()
  221.                 vpfCamera.Parent = newToolButton.ItemViewportFrame
  222.             end
  223.            
  224.             child.AncestryChanged:Connect(function()
  225.                 if child.Parent == nil then
  226.                     newToolButton:Destroy()
  227.                 end
  228.             end)
  229.            
  230.             newToolButton.Parent = tradeFrame.TradingFrame.TheirOfferFrame.Slots
  231.         end)
  232.        
  233.         tradeFrame.Visible = true
  234.     end
  235. end)
  236.  
  237.  
  238. --SEND TRADE REQUESTS
  239. openBtn.MouseButton1Click:Connect(function()
  240.    
  241.     if sendTradesFrame.Visible == true then
  242.         sendTradesFrame.Visible = false
  243.        
  244.     elseif tradeFrame.Visible == false then
  245.        
  246.         for i, child in pairs(sendTradesFrame.PlayerList:GetChildren()) do
  247.             if child:IsA("Frame") then
  248.                 child:Destroy()
  249.             end
  250.         end
  251.        
  252.         for i, plr in pairs(game.Players:GetPlayers()) do
  253.            
  254.             if plr ~= client then
  255.                 local playerFrame = script:WaitForChild("PlayerFrame"):Clone()
  256.                 playerFrame.PlayerDisplayName.Text = plr.DisplayName
  257.                 playerFrame.PlayerUserName.Text = "@" .. plr.Name
  258.                 playerFrame.PlayerImage.Image = game.Players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
  259.                
  260.                 playerFrame.SendButton.MouseButton1Click:Connect(function()
  261.                     if tradeRequestFrame.Visible == false then
  262.                         re:FireServer("send trade request", {plr})
  263.                     end
  264.                 end)
  265.                
  266.                 playerFrame.Parent = sendTradesFrame.PlayerList
  267.             end
  268.         end
  269.        
  270.         sendTradesFrame.Visible = true
  271.     end
  272. end)
  273.  
  274. sendTradesFrame.CloseButton.MouseButton1Click:Connect(function()
  275.     sendTradesFrame.Visible = false
  276. end)
  277.  
  278.  
  279. --ACCEPT OR REJECT TRADE REQUESTS
  280. tradeRequestFrame.RejectButton.MouseButton1Click:Connect(function()
  281.     re:FireServer("reject trade request")
  282. end)
  283.  
  284. tradeRequestFrame.AcceptButton.MouseButton1Click:Connect(function()
  285.     re:FireServer("accept trade request")
  286. end)
  287.  
  288.  
  289. --ACCEPT OR REJECT TRADES
  290. tradeFrame.TradingFrame.RejectButton.MouseButton1Click:Connect(function()
  291.     re:FireServer("reject trade")
  292. end)
  293.  
  294. tradeFrame.TradingFrame.AcceptButton.MouseButton1Click:Connect(function()
  295.     re:FireServer("accept trade")
  296.    
  297.     if tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 == Color3.fromRGB(58, 191, 232) then
  298.         tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 = Color3.fromRGB(40, 109, 152)
  299.     else
  300.         tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 = Color3.fromRGB(58, 191, 232)
  301.     end
  302. end)
Add Comment
Please, Sign In to add comment