totally fine if no one has solution but thought id ask anyways
So I’m in the process of making a trading system and I’m on the part where I’m displaying information about who is offering what, and basically this is the UI
I want the trade list on the top to always be the local players trade offers if that makes sense regardless if they are the sender or if they are the recipient but having a hard time wrapping my head around this
This is the server code
local TradeRequest = {}
TradeRequest.__index = TradeRequest
local Cache = require(game.ReplicatedStorage.Cache)
local timeLeftToRespond = 15
-- This function creates a new TradeRequest object
function TradeRequest.new(sender : Player, recipient : Player)
local self = setmetatable({
sender = sender,
recipient = recipient,
timeLeftToRespond = timeLeftToRespond,
initialTimeLeftToRespond = timeLeftToRespond
}, TradeRequest)
Cache.UpdateTradeRequest(sender, {
sender = sender.Name,
recipient = recipient.Name,
timeLeftToRespond = self.timeLeftToRespond,
initialTimeLeftToRespond = self.timeLeftToRespond
})
task.spawn(function()
self:startTimer()
end)
return self
end
-- This function is called every 0.1 seconds to decrement the timeLeftToRespond
function TradeRequest:startTimer()
local decrementAmount = 1 / 10 -- Decrement timeLeftToRespond by this amount every 0.1 seconds
while self.timeLeftToRespond and self.timeLeftToRespond > 0 and not self.isTradeInProgress do
self.timeLeftToRespond -= decrementAmount
Cache.UpdateTradeRequest(self.sender, {
sender = self.sender.Name,
recipient = self.recipient.Name,
timeLeftToRespond = self.timeLeftToRespond,
initialTimeLeftToRespond = self.initialTimeLeftToRespond
})
task.wait(.1)
end
if self.sender and not self.isTradeInProgress then
self:Decline()
end
end
-- This function is called when the recipient accepts the trade request
function TradeRequest:Accept()
self.isTradeInProgress = true
self.senderCapes = {}
self.recipientCapes = {}
Cache.UpdateTradeRequest(self.sender, {
sender = self.sender.Name,
recipient = self.recipient.Name,
isTradeInProgress = true,
recipientCapes = self.recipientCapes,
senderCapes = self.senderCapes
})
end
-- This function is called when the recipient declines the trade request
function TradeRequest:Decline()
Cache.TradeRequests[tostring(self.sender.UserId)] = nil
Cache.UpdateTradeRequest(self.sender, nil)
for k in pairs(self) do
self[k] = nil
end
end
-- This method is called when either the sender or the recipient adds a cape to the trade
function TradeRequest:AddCape(player : Player, capeName : string)
local function add(tbl)
local myInventory = Cache.Inventories[tostring(player.UserId)]
local hasThisCape = myInventory:DoesThisPlayerHaveThisCape(capeName)
local capeQuantity = myInventory:GetCapeQuantity(capeName)
if not hasThisCape then
return
end
if tbl[capeName] and tbl[capeName].Quantity >= capeQuantity then
print("You don't have enough of this cape to trade")
return
end
if not tbl[capeName] then
tbl[capeName] = {
Quantity = 1
}
else
self.senderCapes[capeName].Quantity += 1
end
Cache.UpdateTradeRequest(self.sender, {
sender = self.sender.Name,
recipient = self.recipient.Name,
isTradeInProgress = true,
recipientCapes = self.recipientCapes,
senderCapes = self.senderCapes
})
print(Cache.TempCache.TradeRequests)
end
if player == self.sender then
add(self.senderCapes)
elseif player == self.recipient then
add(self.recipientCapes)
end
end
return TradeRequest
This is the client code (UI Stuff)
-- Trade Requests
local seenTradeRequests = {}
for _, data in pairs(tradeRequests) do
local sender : string = data.sender
local recipient : string = data.recipient
local isTradeInProgress : boolean = data.isTradeInProgress
-- If the player has a trade request
if recipient == player.Name and not isTradeInProgress then
seenTradeRequests[sender] = true
local template : ImageButton
local senderPlayer : Player = game.Players:FindFirstChild(sender)
if existingTradeRequestsTemplates[sender] then
template = existingTradeRequestsTemplates[sender]
local BottomFrame : Frame = template.BottomFrame
local TopFrame : Frame = BottomFrame.TopFrame
local timeLeftToRespond : number = data.timeLeftToRespond
local initialTimeLeftToRespond : number = data.initialTimeLeftToRespond
local ratio = (timeLeftToRespond / initialTimeLeftToRespond)
spr.target(TopFrame, .9, 2, {
Size = UDim2.new(ratio, 0, TopFrame.Size.Y.Scale, 0)
})
else
template = TradeRequests.PendingRequests.Templates.Template:Clone()
template.Name = sender
template.Parent = TradeRequests.PendingRequests
template.Visible = true
template.displayName.Text = senderPlayer.DisplayName
template.username.Text = "@"..senderPlayer.Name
task.spawn(function()
if TradeRequests.Position.X.Scale <= .99 then return end
TradeRequestTimer += 5
spr.target(TradeRequests, .7, 2.5, {
Position = UDim2.new(.99, 0, .5, 0)
})
task.wait(TradeRequestTimer)
spr.target(TradeRequests, .7, 2.5, {
Position = UDim2.new(1.5, 0, .5, 0)
})
end)
template.Accept.MouseButton1Click:Connect(function()
RemoteManager.acceptTradeRequest:FireServer(sender, true)
end)
template.Decline.MouseButton1Click:Connect(function()
RemoteManager.acceptTradeRequest:FireServer(sender, false)
end)
if not existingThumbnails[senderPlayer.Name] then
local thumbnail = game.Players:GetUserThumbnailAsync(senderPlayer.UserId, Enum.ThumbnailType.HeadShot, Enum.ThumbnailSize.Size180x180)
template.Image.Image = thumbnail
existingThumbnails[senderPlayer.Name] = thumbnail
else
template.Image.Image = existingThumbnails[senderPlayer.Name]
end
existingTradeRequestsTemplates[sender] = template
end
-- If the player is in a trade
elseif recipient == player.Name or sender == player.Name and isTradeInProgress then
if ActiveTrade.Position.Y.Scale >= 1.5 then
spr.target(ActiveTrade, .7, 2.5, {
Position = UDim2.new(.5, 0, .5, 0)
})
end
for capeName, capeData in pairs(playersCapes) do
seenCapes[capeName.."_ActiveTrade"] = true
local template : ImageButton
if existingCapeTemplates[capeName.."_ActiveTrade"] then
template = existingCapeTemplates[capeName.."_ActiveTrade"]
template.Quantity.Text = "x"..capeData.Quantity
else
template = ActiveTrade.MyCapes.Templates.Template:Clone()
template.Name = capeName.."_ActiveTrade"
template.Parent = ActiveTrade.MyCapes
template.Visible = true
template.CapeName.Text = capeName
template.Quantity.Text = "x"..capeData.Quantity
existingCapeTemplates[capeName.."_ActiveTrade"] = template
template.MouseButton1Click:Connect(function()
RemoteManager.addCapeToTrade:FireServer(capeName)
end)
end
end
-- Trade offers
for capeName, capeData in pairs(data.senderCapes) do
seenCapes[capeName.."_Sender"] = true
local template : ImageButton
if existingCapeTemplates[capeName.."_Sender"] then
template = existingCapeTemplates[capeName.."_Sender"]
template.Quantity.Text = "x"..capeData.Quantity
else
template = ActiveTrade.TheirCapes.Templates.Template:Clone()
template.Name = capeName.."_Sender"
template.Parent = ActiveTrade.TheirCapes
template.Visible = true
template.CapeName.Text = capeName
template.Quantity.Text = "x"..capeData.Quantity
existingCapeTemplates[capeName.."_Sender"] = template
end
end
for capeName, capeData in pairs(data.recipientCapes) do
seenCapes[capeName.."_Recipient"] = true
local template : ImageButton
if existingCapeTemplates[capeName.."_Recipient"] then
template = existingCapeTemplates[capeName.."_Recipient"]
template.Quantity.Text = "x"..capeData.Quantity
else
template = ActiveTrade.MyCapes.Templates.Template:Clone()
template.Name = capeName.."_Recipient"
template.Parent = ActiveTrade.MyCapes
template.Visible = true
template.CapeName.Text = capeName
template.Quantity.Text = "x"..capeData.Quantity
existingCapeTemplates[capeName.."_Recipient"] = template
end
end
end
end