Hey everyone I’m currently working on an Inventory and Trading System and know I want to connect both with each other, I mean that if I trade a tool with someone, the tool I got should appear in my Inventory.I got some scripts(not all are important I only need the function) I think these are the right one’s(I have more)I just need to change the function that if I get a tool it appears in my hotBar I want it to appear instantly in my Inventory:
The InventoryLocalScript:
local InventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
local itemFrame = script.Parent:FindFirstChild(“ItemsFrame”)
InventoryEvent.OnClientEvent:Connect(function(ItemName, Value)
if Value == true then
local ItemButton = script.Parent.ItemsFrame.ItemButton:Clone()
ItemButton.Visible = true
ItemButton.Name = ItemName
ItemButton.Text = ItemName
ItemButton.Parent = itemFrame
local equipButton = script.Parent.EquipFrame["EquipButton"]
ItemButton.MouseButton1Click:Connect(function()
script.Parent.EquipFrame.Title.Text = ItemName
script.Parent.EquipFrame.Title.Visible = true
equipButton.Visible = true
end)
end
end)
The TradeLocalScript:
–VARIABLES
local client = game.Players.LocalPlayer
local rs = game.ReplicatedStorage:WaitForChild(“TradeReplicatedStorage”)
local re = rs:WaitForChild(“RemoteEvent”)
local config = require(rs:WaitForChild(“CONFIGURATION”))
local tradeRequestsFolder = rs:WaitForChild(“TRADE REQUESTS”)
local ongoingTradesFolder = rs:WaitForChild(“ONGOING TRADES”)
local gui = script.Parent
local openBtn = gui:WaitForChild(“OpenSendTrades”)
local sendTradesFrame = gui:WaitForChild(“SendTradesFrame”)
local tradeRequestFrame = gui:WaitForChild(“TradeRequestFrame”)
local tradeFrame = gui:WaitForChild(“TradeFrame”)
sendTradesFrame.Visible = false
tradeRequestFrame.Visible = false
tradeFrame.Visible = false
–TRADE REQUESTS
tradeRequestsFolder.ChildAdded:Connect(function(child)
if child.Value == client.Name then
tradeRequestFrame.TradeText.Text = child.Name .. " sent you a trade request!"
tradeRequestFrame.AcceptButton.Visible = true
tradeRequestFrame.RejectButton.Visible = true
tradeRequestFrame.Visible = true
elseif child.Name == client.Name then
tradeRequestFrame.TradeText.Text = "You sent a trade request to " .. child.Value
tradeRequestFrame.AcceptButton.Visible = false
tradeRequestFrame.RejectButton.Visible = true
tradeRequestFrame.Visible = true
end
end)
tradeRequestsFolder.ChildRemoved:Connect(function(child)
if child.Value == client.Name or child.Name == client.Name then
tradeRequestFrame.Visible = false
end
end)
–ONGOING TRADES
ongoingTradesFolder.ChildAdded:Connect(function(child)
if child:WaitForChild("Sender").Value == client.Name or child:WaitForChild("Receiver").Value == client.Name then
local clientValue = child:WaitForChild("Sender").Value == client.Name and child.Sender or child.Receiver
local otherPlrValue = clientValue.Name == "Sender" and child.Receiver or child.Sender
clientValue.AncestryChanged:Connect(function()
if clientValue.Parent == nil then
tradeFrame.Visible = false
openBtn.Visible = true
end
end)
tradeRequestFrame.Visible = false
sendTradesFrame.Visible = false
openBtn.Visible = false
tradeFrame.TradingFrame.TradingWithName.Text = "Trading with " .. otherPlrValue.Value
tradeFrame.TradingFrame.TheirOfferFrame.TheirOfferText.Text = otherPlrValue.Value .. "'s offer"
tradeFrame.TradingFrame.PlayerAccepted.Text = ""
tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 = Color3.fromRGB(58, 191, 232)
for i, child in pairs(tradeFrame.TradingFrame.YourOfferFrame:GetChildren()) do
if child:IsA("TextButton") or child:IsA("ImageButton") or child:IsA("Frame") then
child:Destroy()
end
end
for i, child in pairs(tradeFrame.TradingFrame.TheirOfferFrame:GetChildren()) do
if child:IsA("TextButton") or child:IsA("ImageButton") or child:IsA("Frame") then
child:Destroy()
end
end
--Alert client when other player has accepted
otherPlrValue.ChildAdded:Connect(function(child)
if child.Name == "ACCEPTED" then
tradeFrame.TradingFrame.PlayerAccepted.Text = otherPlrValue.Value .. " has accepted"
end
end)
otherPlrValue.ChildRemoved:Connect(function(child)
if child.Name == "ACCEPTED" then
tradeFrame.TradingFrame.PlayerAccepted.Text = ""
end
end)
--Display player's inventory
local inventoryList = tradeFrame.InventoryFrame.InventoryList
for i, child in pairs(inventoryList:GetChildren()) do
if child:IsA("TextButton") or child:IsA("ImageButton") then
child:Destroy()
end
end
local clientTools = config.GetTools(client)
for i, tool in pairs(clientTools) do
local newToolButton = script:WaitForChild("ItemButton"):Clone()
newToolButton.Name = tool["TRADING ID"].Value
newToolButton.ItemName.Text = tool.Name
newToolButton.IsInTrade.Visible = false
if string.len(tool.TextureId) > 0 then
newToolButton.ItemViewportFrame.Visible = false
newToolButton.ItemImageLabel.Image = tool.TextureId
else
newToolButton.ItemImageLabel.Visible = false
local toolModel = Instance.new("Model")
for x, descendant in pairs(tool:GetDescendants()) do
if descendant:IsA("BasePart") then
descendant:Clone().Parent = toolModel
end
end
toolModel.PrimaryPart = toolModel:FindFirstChild("Handle") or toolModel:FindFirstChildOfClass("BasePart", true)
toolModel:SetPrimaryPartCFrame(CFrame.new(0, 0, -3))
toolModel.Parent = newToolButton.ItemViewportFrame
local vpfCamera = Instance.new("Camera")
vpfCamera.CFrame = CFrame.new()
vpfCamera.Parent = newToolButton.ItemViewportFrame
end
newToolButton.MouseButton1Click:Connect(function()
if not tradeFrame.TradingFrame.YourOfferFrame.Slots:FindFirstChild(tool["TRADING ID"].Value) then
newToolButton.IsInTrade.Visible = true
re:FireServer("add item to trade", {tool})
else
newToolButton.IsInTrade.Visible = false
re:FireServer("remove item from trade", {tool})
end
end)
newToolButton.Parent = inventoryList
end
--Display client's offer
local clientOffer = child[clientValue.Value .. "'s offer"]
clientOffer.ChildAdded:Connect(function(child)
local newToolButton = inventoryList[child:WaitForChild("TRADING ID").Value]:Clone()
newToolButton.IsInTrade.Visible = false
newToolButton.Size = script.ItemButton.Size
newToolButton.MouseButton1Click:Connect(function()
local plrTools = config.GetTools(client)
for i, plrTool in pairs(plrTools) do
if child["TRADING ID"].Value == plrTool["TRADING ID"].Value then
re:FireServer("remove item from trade", {plrTool})
break
end
end
end)
child.AncestryChanged:Connect(function()
if child.Parent == nil then
tradeFrame.InventoryFrame.InventoryList[newToolButton.Name].IsInTrade.Visible = false
newToolButton:Destroy()
end
end)
newToolButton.Parent = tradeFrame.TradingFrame.YourOfferFrame.Slots
end)
--Display other player's offer
local otherPlrOffer = child[otherPlrValue.Value .. "'s offer"]
otherPlrOffer.ChildAdded:Connect(function(child)
local newToolButton = script.ItemButton:Clone()
newToolButton.Name = child:WaitForChild("TRADING ID").Value
newToolButton.ItemName.Text = child.Name
newToolButton.IsInTrade.Visible = false
newToolButton.AutoButtonColor = false
if string.len(child.TextureId) > 0 then
newToolButton.ItemViewportFrame.Visible = false
newToolButton.ItemImageLabel.Image = child.TextureId
else
newToolButton.ItemImageLabel.Visible = false
local toolModel = Instance.new("Model")
for x, descendant in pairs(child:GetDescendants()) do
if descendant:IsA("BasePart") then
descendant:Clone().Parent = toolModel
end
end
toolModel.PrimaryPart = toolModel:FindFirstChild("Handle") or toolModel:FindFirstChildOfClass("BasePart", true)
toolModel:SetPrimaryPartCFrame(CFrame.new(0, 0, -3))
toolModel.Parent = newToolButton.ItemViewportFrame
local vpfCamera = Instance.new("Camera")
vpfCamera.CFrame = CFrame.new()
vpfCamera.Parent = newToolButton.ItemViewportFrame
end
child.AncestryChanged:Connect(function()
if child.Parent == nil then
newToolButton:Destroy()
end
end)
newToolButton.Parent = tradeFrame.TradingFrame.TheirOfferFrame.Slots
end)
tradeFrame.Visible = true
end
end)
–SEND TRADE REQUESTS
openBtn.MouseButton1Click:Connect(function()
if sendTradesFrame.Visible == true then
sendTradesFrame.Visible = false
elseif tradeFrame.Visible == false then
for i, child in pairs(sendTradesFrame.PlayerList:GetChildren()) do
if child:IsA("Frame") then
child:Destroy()
end
end
for i, plr in pairs(game.Players:GetPlayers()) do
if plr ~= client then
local playerFrame = script:WaitForChild("PlayerFrame"):Clone()
playerFrame.PlayerDisplayName.Text = plr.DisplayName
playerFrame.PlayerUserName.Text = "@" .. plr.Name
playerFrame.PlayerImage.Image = game.Players:GetUserThumbnailAsync(plr.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size100x100)
playerFrame.SendButton.MouseButton1Click:Connect(function()
if tradeRequestFrame.Visible == false then
re:FireServer("send trade request", {plr})
end
end)
playerFrame.Parent = sendTradesFrame.PlayerList
end
end
sendTradesFrame.Visible = true
end
end)
sendTradesFrame.CloseButton.MouseButton1Click:Connect(function()
sendTradesFrame.Visible = false
end)
–ACCEPT OR REJECT TRADE REQUESTS
tradeRequestFrame.RejectButton.MouseButton1Click:Connect(function()
re:FireServer(“reject trade request”)
end)
tradeRequestFrame.AcceptButton.MouseButton1Click:Connect(function()
re:FireServer(“accept trade request”)
end)
–ACCEPT OR REJECT TRADES
tradeFrame.TradingFrame.RejectButton.MouseButton1Click:Connect(function()
re:FireServer(“reject trade”)
end)
tradeFrame.TradingFrame.AcceptButton.MouseButton1Click:Connect(function()
re:FireServer(“accept trade”)
if tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 == Color3.fromRGB(58, 191, 232) then
tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 = Color3.fromRGB(40, 109, 152)
else
tradeFrame.TradingFrame.AcceptButton.BackgroundColor3 = Color3.fromRGB(58, 191, 232)
end
end)
InventoryScript:
local inventoryEvent = game.ReplicatedStorage.Remotes.InventoryEvent
game.Players.PlayerAdded:Connect(function(player)
local inventory = player:WaitForChild("Inventory")
local inventoryFrame = player.PlayerGui:WaitForChild("InventoryGui").InventoryFrame.ItemsFrame:GetChildren()
inventory.ChildAdded:Connect(function(Item)
inventoryEvent:FireClient(player, Item.Name, true)
end)
end)
inventoryEvent.OnServerEvent:Connect(function(player, ItemName, Value, button)
if Value == false then
local SelectedItem = player.Inventory:FindFirstChild(ItemName)
local backpack = player.Backpack:GetChildren()
local stuff = player.Character:GetChildren()
if #backpack == 0 and not player.Character:FindFirstChildWhichIsA("Tool") then
button.Text = "Unequip"
button.BackgroundColor3 = Color3.new(255,0,0)
SelectedItem:Clone().Parent = player.Backpack
else
for i,v in ipairs(backpack) do
button.Text = "Equip"
button.BackgroundColor3 = Color3.new(0,255,0)
v:Destroy()
end
for i, v in ipairs(stuff) do
if v:IsA("Tool") then
button.Text = "Equip"
button.BackgroundColor3 = Color3.new(0,255,0)
v:Destroy()
end
end
end
end
end)
TradingScript:
–VARIABLES
local rs = game.ReplicatedStorage:WaitForChild(“TradeReplicatedStorage”)
local re = rs:WaitForChild(“RemoteEvent”)
local config = require(rs:WaitForChild(“CONFIGURATION”))
local tradeRequestsFolder = Instance.new(“Folder”)
tradeRequestsFolder.Name = “TRADE REQUESTS”
tradeRequestsFolder.Parent = rs
local ongoingTradesFolder = Instance.new(“Folder”)
ongoingTradesFolder.Name = “ONGOING TRADES”
ongoingTradesFolder.Parent = rs
–REMOVE TRADES FOR THIS PLAYER
function removeTrades(plr)
for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
– Return tools to the player’s Backpack if they were in the trade
local senderPlr = game.Players[trade.Sender.Value]
local receiverPlr = game.Players[trade.Receiver.Value]
local senderSlots = trade[senderPlr.Name … “'s offer”]
local receiverSlots = trade[receiverPlr.Name … “'s offer”]
for _, slot in ipairs(senderSlots:GetChildren()) do
local tool = slot:FindFirstChildOfClass("Tool")
if tool then
tool.Parent = senderPlr.Backpack
end
end
for _, slot in ipairs(receiverSlots:GetChildren()) do
local tool = slot:FindFirstChildOfClass("Tool")
if tool then
tool.Parent = receiverPlr.Backpack
end
end
trade:Destroy()
end
end
for i, request in pairs(tradeRequestsFolder:GetChildren()) do
if request.Name == plr.Name or request.Value == plr.Name then
request:Destroy()
end
end
end
–REMOVE TRADES WHEN PLAYER DIES
game.Players.PlayerAdded:Connect(function(plr)
plr.CharacterAdded:Connect(function(char)
char:WaitForChild(“Humanoid”).Died:Connect(function()
removeTrades(plr)
end)
end)
end)
–REMOVE TRADES WHEN PLAYER LEAVES
game.Players.PlayerRemoving:Connect(removeTrades)
–RECEIVE CLIENT INFORMATION
re.OnServerEvent:Connect(function(plr, instruction, data)
--Send a request
if instruction == "send trade request" then
local playerSent = data[1]
if playerSent and playerSent ~= plr then
local inTrade = false
for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
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
inTrade = true
break
end
end
for i, request in pairs(tradeRequestsFolder:GetChildren()) do
if request.Name == playerSent.Name or request.Name == plr.Name or request.Value == playerSent.Name or request.Value == plr.Name then
inTrade = true
break
end
end
if not inTrade then
local newRequest = Instance.new("StringValue")
newRequest.Name = plr.Name
newRequest.Value = playerSent.Name
newRequest.Parent = tradeRequestsFolder
end
end
--Reject a request
elseif instruction == "reject trade request" then
local requestValue = nil
for i, request in pairs(tradeRequestsFolder:GetChildren()) do
if request.Name == plr.Name or request.Value == plr.Name then
requestValue = request
break
end
end
if requestValue.Parent == tradeRequestsFolder and requestValue.Name == plr.Name or requestValue.Value == plr.Name then
requestValue:Destroy()
end
--Accept a request
elseif instruction == "accept trade request" then
local requestValue = nil
for i, request in pairs(tradeRequestsFolder:GetChildren()) do
if request.Name == plr.Name or request.Value == plr.Name then
requestValue = request
break
end
end
if requestValue.Parent == tradeRequestsFolder and requestValue.Value == plr.Name then
local senderPlr = game.Players[requestValue.Name]
local receiverPlr = game.Players[requestValue.Value]
requestValue:Destroy()
local tradeFolder = Instance.new("Folder")
local senderValue = Instance.new("StringValue")
senderValue.Name = "Sender"
senderValue.Value = senderPlr.Name
senderValue.Parent = tradeFolder
local receiverValue = Instance.new("StringValue")
receiverValue.Name = "Receiver"
receiverValue.Value = receiverPlr.Name
receiverValue.Parent = tradeFolder
local senderOffer = Instance.new("Folder")
senderOffer.Name = senderPlr.Name .. "'s offer"
senderOffer.Parent = tradeFolder
local receiverOffer = Instance.new("Folder")
receiverOffer.Name = receiverPlr.Name .. "'s offer"
receiverOffer.Parent = tradeFolder
tradeFolder.Parent = ongoingTradesFolder
local toolIds = {}
local senderTools = config.GetTools(senderPlr)
for i, tool in pairs(senderTools) do
tool.CanBeDropped = false
local toolId = tool:FindFirstChild("TRADING ID") or Instance.new("NumberValue")
toolId.Name = "TRADING ID"
while string.len(tostring(toolId.Value)) < 1 or table.find(toolIds, toolId.Value) do
toolId.Value = Random.new():NextNumber(0, 1000000)
end
table.insert(toolIds, toolId.Value)
toolId.Parent = tool
end
local receiverTools = config.GetTools(receiverPlr)
for i, tool in pairs(receiverTools) do
tool.CanBeDropped = false
local toolId = tool:FindFirstChild("TRADING ID") or Instance.new("NumberValue")
toolId.Name = "TRADING ID"
while string.len(tostring(toolId.Value)) < 1 or table.find(toolIds, toolId.Value) do
toolId.Value = Random.new():NextNumber(0, 1000000)
end
table.insert(toolIds, toolId.Value)
toolId.Parent = tool
end
end
--Add an item to the trade
elseif instruction == "add item to trade" then
local item = data[1]
if item.Parent == plr.Backpack or item.Parent == plr.Character then
local currentTrade = nil
for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
currentTrade = trade
break
end
end
if currentTrade then
local plrSlots = currentTrade[plr.Name .. "'s offer"]
local numItems = #plrSlots:GetChildren()
if numItems < config.MaxSlots then
local itemInTrade = false
for i, plrItem in pairs(plrSlots:GetChildren()) do
if plrItem["TRADING ID"].Value == item["TRADING ID"].Value then
itemInTrade = true
break
end
end
if not itemInTrade then
if currentTrade.Receiver:FindFirstChild("ACCEPTED") then
currentTrade.Receiver.ACCEPTED:Destroy()
end
if currentTrade.Sender:FindFirstChild("ACCEPTED") then
currentTrade.Sender.ACCEPTED:Destroy()
end
item:Clone().Parent = plrSlots
end
end
end
end
--Remove an item from the trade
elseif instruction == "remove item from trade" then
local item = data[1]
if item.Parent == plr.Backpack or item.Parent == plr.Character then
local currentTrade = nil
for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
currentTrade = trade
break
end
end
if currentTrade then
local plrSlots = currentTrade[plr.Name .. "'s offer"]
for i, plrItem in pairs(plrSlots:GetChildren()) do
if plrItem["TRADING ID"].Value == item["TRADING ID"].Value then
if currentTrade.Receiver:FindFirstChild("ACCEPTED") then
currentTrade.Receiver.ACCEPTED:Destroy()
end
if currentTrade.Sender:FindFirstChild("ACCEPTED") then
currentTrade.Sender.ACCEPTED:Destroy()
end
plrItem:Destroy()
break
end
end
end
end
--Accept a trade
elseif instruction == "accept trade" then
local currentTrade = nil
for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
currentTrade = trade
break
end
end
if currentTrade then
local plrValue = currentTrade.Sender.Value == plr.Name and currentTrade.Sender or currentTrade.Receiver.Value == plr.Name and currentTrade.Receiver
if plrValue then
if not plrValue:FindFirstChild("ACCEPTED") then
local acceptedValue = Instance.new("StringValue")
acceptedValue.Name = "ACCEPTED"
acceptedValue.Parent = plrValue
else
plrValue.ACCEPTED:Destroy()
end
end
if currentTrade.Sender:FindFirstChild("ACCEPTED") and currentTrade.Receiver:FindFirstChild("ACCEPTED") then
task.wait(config.TimeBeforeTradeConfirmed)
if currentTrade.Sender:FindFirstChild("ACCEPTED") and currentTrade.Receiver:FindFirstChild("ACCEPTED") then
local senderPlr = game.Players[currentTrade.Sender.Value]
local senderSlots = currentTrade[senderPlr.Name .. "'s offer"]
local receiverPlr = game.Players[currentTrade.Receiver.Value]
local receiverSlots = currentTrade[receiverPlr.Name .. "'s offer"]
local senderTools = config.GetTools(senderPlr)
local receiverTools = config.GetTools(receiverPlr)
for i, senderTool in pairs(senderTools) do
for x, senderSlot in pairs(senderSlots:GetChildren()) do
if senderTool["TRADING ID"].Value == senderSlot["TRADING ID"].Value then
senderTool.Parent = receiverPlr.Backpack
end
end
end
for i, receiverTool in pairs(receiverTools) do
for x, receiverSlot in pairs(receiverSlots:GetChildren()) do
if receiverTool["TRADING ID"].Value == receiverSlot["TRADING ID"].Value then
receiverTool.Parent = senderPlr.Backpack
end
end
end
currentTrade:Destroy()
end
end
end
--Reject a trade
elseif instruction == "reject trade" then
for i, trade in pairs(ongoingTradesFolder:GetChildren()) do
if trade.Sender.Value == plr.Name or trade.Receiver.Value == plr.Name then
trade:Destroy()
break
end
end
end
end)