Advertisement
HowToRoblox

TradeServer

Nov 20th, 2022
1,851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 9.34 KB | None | 0 0
  1. --VARIABLES
  2. local rs = game.ReplicatedStorage:WaitForChild("TradeReplicatedStorage")
  3. local re = rs:WaitForChild("RemoteEvent")
  4. local config = require(rs:WaitForChild("CONFIGURATION"))
  5.  
  6. local tradeRequestsFolder = Instance.new("Folder")
  7. tradeRequestsFolder.Name = "TRADE REQUESTS"
  8. tradeRequestsFolder.Parent = rs
  9.  
  10. local ongoingTradesFolder = Instance.new("Folder")
  11. ongoingTradesFolder.Name = "ONGOING TRADES"
  12. ongoingTradesFolder.Parent = rs
  13.  
  14.  
  15. --REMOVE TRADES FOR THIS PLAYER
  16. function removeTrades(plr)
  17.     for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
  18.         if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
  19.             trade:Destroy()
  20.         end
  21.     end
  22.  
  23.     for i, request in pairs(tradeRequestsFolder:GetChildren()) do
  24.         if request.Name == plr.Name or request.Value == plr.Name then
  25.             request:Destroy()
  26.         end
  27.     end
  28. end
  29.  
  30.  
  31. --REMOVE TRADES WHEN PLAYER DIES
  32. game.Players.PlayerAdded:Connect(function(plr)
  33.     plr.CharacterAdded:Connect(function(char)
  34.        
  35.         char:WaitForChild("Humanoid").Died:Connect(function()
  36.             removeTrades(plr)
  37.         end)
  38.     end)
  39. end)
  40.  
  41. --REMOVE TRADES WHEN PLAYER LEAVES
  42. game.Players.PlayerRemoving:Connect(removeTrades)
  43.  
  44.  
  45. --RECEIVE CLIENT INFORMATION
  46. re.OnServerEvent:Connect(function(plr, instruction, data)
  47.    
  48.     --Send a request
  49.     if instruction == "send trade request" then
  50.        
  51.         local playerSent = data[1]
  52.        
  53.         if playerSent and playerSent ~= plr then
  54.             local inTrade = false
  55.            
  56.             for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
  57.                 if trade.Sender.Value == playerSent.Name or trade.Sender.Value == plr.Name or trade.Receiever.Value == playerSent.Name or trade.Receiver.Value == plr.Name then
  58.                     inTrade = true
  59.                     break
  60.                 end
  61.             end
  62.            
  63.             for i, request in pairs(tradeRequestsFolder:GetChildren()) do
  64.                 if request.Name == playerSent.Name or request.Name == plr.Name or request.Value == playerSent.Name or request.Value == plr.Name then
  65.                     inTrade = true
  66.                     break
  67.                 end
  68.             end
  69.            
  70.             if not inTrade then
  71.                
  72.                 local newRequest = Instance.new("StringValue")
  73.                 newRequest.Name = plr.Name
  74.                 newRequest.Value = playerSent.Name
  75.                 newRequest.Parent = tradeRequestsFolder
  76.             end
  77.         end
  78.        
  79.        
  80.     --Reject a request
  81.     elseif instruction == "reject trade request" then
  82.        
  83.         local requestValue = nil
  84.         for i, request in pairs(tradeRequestsFolder:GetChildren()) do
  85.             if request.Name == plr.Name or request.Value == plr.Name then
  86.                 requestValue = request
  87.                 break
  88.             end
  89.         end
  90.        
  91.         if requestValue.Parent == tradeRequestsFolder and requestValue.Name == plr.Name or requestValue.Value == plr.Name then
  92.             requestValue:Destroy()
  93.         end
  94.        
  95.        
  96.     --Accept a request
  97.     elseif instruction == "accept trade request" then
  98.        
  99.         local requestValue = nil
  100.         for i, request in pairs(tradeRequestsFolder:GetChildren()) do
  101.             if request.Name == plr.Name or request.Value == plr.Name then
  102.                 requestValue = request
  103.                 break
  104.             end
  105.         end
  106.        
  107.         if requestValue.Parent == tradeRequestsFolder and requestValue.Value == plr.Name then
  108.            
  109.             local senderPlr = game.Players[requestValue.Name]
  110.             local receiverPlr = game.Players[requestValue.Value]
  111.            
  112.            
  113.             requestValue:Destroy()
  114.            
  115.             local tradeFolder = Instance.new("Folder")
  116.            
  117.             local senderValue = Instance.new("StringValue")
  118.             senderValue.Name = "Sender"
  119.             senderValue.Value = senderPlr.Name
  120.             senderValue.Parent = tradeFolder
  121.            
  122.             local receiverValue = Instance.new("StringValue")
  123.             receiverValue.Name = "Receiver"
  124.             receiverValue.Value = receiverPlr.Name
  125.             receiverValue.Parent = tradeFolder
  126.            
  127.             local senderOffer = Instance.new("Folder")
  128.             senderOffer.Name = senderPlr.Name .. "'s offer"
  129.             senderOffer.Parent = tradeFolder
  130.            
  131.             local receiverOffer = Instance.new("Folder")
  132.             receiverOffer.Name = receiverPlr.Name .. "'s offer"
  133.             receiverOffer.Parent = tradeFolder
  134.            
  135.             tradeFolder.Parent = ongoingTradesFolder
  136.            
  137.            
  138.             local toolIds = {}
  139.            
  140.             local senderTools = config.GetTools(senderPlr)
  141.             for i, tool in pairs(senderTools) do
  142.                 tool.CanBeDropped = false
  143.                
  144.                 local toolId = tool:FindFirstChild("TRADING ID") or Instance.new("NumberValue")
  145.                 toolId.Name = "TRADING ID"
  146.                
  147.                 while string.len(tostring(toolId.Value)) < 1 or table.find(toolIds, toolId.Value) do
  148.                     toolId.Value = Random.new():NextNumber(0, 1000000)
  149.                 end
  150.                 table.insert(toolIds, toolId.Value)
  151.                
  152.                 toolId.Parent = tool
  153.             end
  154.            
  155.             local receiverTools = config.GetTools(receiverPlr)
  156.             for i, tool in pairs(receiverTools) do
  157.                 tool.CanBeDropped = false
  158.  
  159.                 local toolId = tool:FindFirstChild("TRADING ID") or Instance.new("NumberValue")
  160.                 toolId.Name = "TRADING ID"
  161.  
  162.                 while string.len(tostring(toolId.Value)) < 1 or table.find(toolIds, toolId.Value) do
  163.                     toolId.Value = Random.new():NextNumber(0, 1000000)
  164.                 end
  165.                 table.insert(toolIds, toolId.Value)
  166.  
  167.                 toolId.Parent = tool
  168.             end
  169.         end
  170.        
  171.        
  172.     --Add an item to the trade
  173.     elseif instruction == "add item to trade" then
  174.        
  175.         local item = data[1]
  176.        
  177.         if item.Parent == plr.Backpack or item.Parent == plr.Character then
  178.            
  179.             local currentTrade = nil
  180.             for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
  181.                 if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
  182.                     currentTrade = trade
  183.                     break
  184.                 end
  185.             end
  186.            
  187.             if currentTrade then
  188.                
  189.                 local plrSlots = currentTrade[plr.Name .. "'s offer"]
  190.                 local numItems = #plrSlots:GetChildren()
  191.                
  192.                 if numItems < config.MaxSlots then
  193.                    
  194.                     local itemInTrade = false
  195.                    
  196.                     for i, plrItem in pairs(plrSlots:GetChildren()) do
  197.                         if plrItem["TRADING ID"].Value == item["TRADING ID"].Value then
  198.                             itemInTrade = true
  199.                             break
  200.                         end
  201.                     end
  202.                    
  203.                     if not itemInTrade then
  204.                        
  205.                         if currentTrade.Receiver:FindFirstChild("ACCEPTED") then
  206.                             currentTrade.Receiver.ACCEPTED:Destroy()
  207.                         end
  208.                         if currentTrade.Sender:FindFirstChild("ACCEPTED") then
  209.                             currentTrade.Sender.ACCEPTED:Destroy()
  210.                         end
  211.                        
  212.                         item:Clone().Parent = plrSlots
  213.                     end
  214.                 end
  215.             end
  216.         end
  217.        
  218.        
  219.     --Remove an item from the trade
  220.     elseif instruction == "remove item from trade" then
  221.        
  222.         local item = data[1]
  223.        
  224.         if item.Parent == plr.Backpack or item.Parent == plr.Character then
  225.            
  226.             local currentTrade = nil
  227.             for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
  228.                 if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
  229.                     currentTrade = trade
  230.                     break
  231.                 end
  232.             end
  233.            
  234.             if currentTrade then
  235.                
  236.                 local plrSlots = currentTrade[plr.Name .. "'s offer"]
  237.                
  238.                 for i, plrItem in pairs(plrSlots:GetChildren()) do
  239.                     if plrItem["TRADING ID"].Value == item["TRADING ID"].Value then
  240.                        
  241.                         if currentTrade.Receiver:FindFirstChild("ACCEPTED") then
  242.                             currentTrade.Receiver.ACCEPTED:Destroy()
  243.                         end
  244.                         if currentTrade.Sender:FindFirstChild("ACCEPTED") then
  245.                             currentTrade.Sender.ACCEPTED:Destroy()
  246.                         end
  247.                        
  248.                         plrItem:Destroy()
  249.                         break
  250.                     end
  251.                 end
  252.             end
  253.         end
  254.        
  255.        
  256.     --Accept a trade
  257.     elseif instruction == "accept trade" then
  258.        
  259.         local currentTrade = nil
  260.         for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
  261.             if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
  262.                 currentTrade = trade
  263.                 break
  264.             end
  265.         end
  266.        
  267.         if currentTrade then
  268.             local plrValue = currentTrade.Sender.Value == plr.Name and currentTrade.Sender or currentTrade.Receiver.Value == plr.Name and currentTrade.Receiver
  269.            
  270.             if plrValue then
  271.                
  272.                 if not plrValue:FindFirstChild("ACCEPTED") then
  273.                     local acceptedValue = Instance.new("StringValue")
  274.                     acceptedValue.Name = "ACCEPTED"
  275.                     acceptedValue.Parent = plrValue
  276.                    
  277.                 else
  278.                     plrValue.ACCEPTED:Destroy()
  279.                 end
  280.             end
  281.            
  282.             if currentTrade.Sender:FindFirstChild("ACCEPTED") and currentTrade.Receiver:FindFirstChild("ACCEPTED") then
  283.                
  284.                 task.wait(config.TimeBeforeTradeConfirmed)
  285.                
  286.                 if currentTrade.Sender:FindFirstChild("ACCEPTED") and currentTrade.Receiver:FindFirstChild("ACCEPTED") then
  287.                    
  288.                     local senderPlr = game.Players[currentTrade.Sender.Value]
  289.                     local senderSlots = currentTrade[senderPlr.Name .. "'s offer"]
  290.                    
  291.                     local receiverPlr = game.Players[currentTrade.Receiver.Value]
  292.                     local receiverSlots = currentTrade[receiverPlr.Name .. "'s offer"]
  293.                    
  294.                     local senderTools = config.GetTools(senderPlr)
  295.                     local receiverTools = config.GetTools(receiverPlr)
  296.                    
  297.                     for i, senderTool in pairs(senderTools) do
  298.                         for x, senderSlot in pairs(senderSlots:GetChildren()) do
  299.                             if senderTool["TRADING ID"].Value == senderSlot["TRADING ID"].Value then
  300.                                 senderTool.Parent = receiverPlr.Backpack
  301.                             end
  302.                         end
  303.                     end
  304.                    
  305.                     for i, receiverTool in pairs(receiverTools) do
  306.                         for x, receiverSlot in pairs(receiverSlots:GetChildren()) do
  307.                             if receiverTool["TRADING ID"].Value == receiverSlot["TRADING ID"].Value then
  308.                                 receiverTool.Parent = senderPlr.Backpack
  309.                             end
  310.                         end
  311.                     end
  312.                    
  313.                     currentTrade:Destroy()
  314.                 end
  315.             end
  316.         end
  317.        
  318.        
  319.     --Reject a trade
  320.     elseif instruction == "reject trade" then
  321.  
  322.         for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
  323.            
  324.             if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then     
  325.                 trade:Destroy()
  326.                
  327.                 break
  328.             end
  329.         end
  330.     end
  331. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement