So I have a trading system and I have a veryyy big problem.
If 2 people are in a trade and then 2 more people get in a trade no one can accept the trade(Not the trade request, the actual trade)
So dunno if anyone is going to go through all my code but if you could figure it out its worth a shot! :3
local id = 0
tradeRequestEvent.OnServerEvent:Connect(function(player,user:Player)
local InTrade = false
for name, trade in pairs(trades) do -- get names for both users and trade
local names = string.split(name,"_")
for _, n in pairs(names) do
if n == player.Name or n == user.Name then
InTrade = true
end
end
end
if not InTrade then -- make sure they are not in a trade
tradeAskFor:FireClient(user,player)
local finished = false
local tradeAccepted = false
local connectZ = tradeAskFor.OnServerEvent:Connect(function(invited,accepted,plr) -- wait for them to accept
if accepted and plr == player then
if accepted == 1 then
tradeAccepted = false
else
tradeAccepted = true
end
finished = true
end
end)
local c = 0
repeat task.wait(0.5); c += 0.5 until c >= TimeToAccept or finished -- either accepted or declined or they didnt do it in X amount of time
connectZ:Disconnect()
if tradeAccepted and not trades[player.Name..'_'..user.Name] then -- see if the trade is accepted and the trade of the same name doesnt exist
id += 1
local current = id
local trade = { -- create the trade
Plr1 = { -- player 1/ Player
Plr = player,
Items = {},
Accepted = false,
},
Plr2 = { -- player 2/ user
Plr = user,
Items = {},
Accepted = false,
},
TimeTilTrade = module.Details.TimeToDecline -- time for the user to unaccept said trade
}
trades[player.Name..'_'..user.Name] = trade -- finish by creating the trade!
-- update both users
TradeUpdate:FireClient(player,trades[player.Name..'_'..user.Name])
TradeUpdate:FireClient(user,trades[player.Name..'_'..user.Name])
local connect = TradeUpdate.OnServerEvent:Connect(function(plr,trade,Update,other) -- this triggers every time an update is fired.
print('Trade update received: '..Update)
warn(trade)
if trades[trade.Plr1.Plr.Name..'_'..trade.Plr2.Plr.Name] then -- see which trade we are handling
local actualTrade = trades[trade.Plr1.Plr.Name..'_'..trade.Plr2.Plr.Name] -- get the actual trade
local play = actualTrade.Plr1 -- this value is to determine who fired this update, whether it was P1 or P2
if actualTrade.Plr1.Plr == plr then
play = actualTrade.Plr1
else
play = actualTrade.Plr2
end
if Update == 1 then -- declined trade
trades[trade.Plr1.Plr.Name..'_'..trade.Plr2.Plr.Name] = nil
TradeUpdate:FireClient(player,nil)
TradeUpdate:FireClient(user,nil)
elseif Update == 2 then -- accepted
if actualTrade.Plr1.Items or actualTrade.Plr2.Items then -- make sure that there is items in the trade, otherwise we arent aloud to accept it
play.Accepted = not play.Accepted
else
play.Accepted = false
end
TradeUpdate:FireClient(player,actualTrade)
TradeUpdate:FireClient(user,actualTrade)
elseif Update == 3 then -- add items
play.Items = other -- add the items into their table
TradeUpdate:FireClient(player,actualTrade)
TradeUpdate:FireClient(user,actualTrade)
end
local z = true
if actualTrade.Plr1.Accepted and actualTrade.Plr2.Accepted and z then -- see if both trades are accpeted
local changed = false
repeat -- wait X amount of time then if the trade was not declined then,
wait(1)
actualTrade.TimeTilTrade -= 1
TradeUpdate:FireClient(player,actualTrade)
TradeUpdate:FireClient(user,actualTrade)
if not actualTrade.Plr1.Accepted or not actualTrade.Plr2.Accepted then
if not actualTrade.Plr1 or not actualTrade.Plr2 then
actualTrade.TimeTilTrade = module.Details.TimeToDecline
changed = true
end
end
until actualTrade.TimeTilTrade <= 0 or changed
if not changed then -- make sure that nothing changed that way we can carry on with this trade
if actualTrade.Plr1.Items or actualTrade.Plr2.Items then
-- transfer items!
for name, item in pairs(actualTrade.Plr1.Items) do
item.Parent = actualTrade.Plr2.Plr:FindFirstChild('Pets')
end
for name, item in pairs(actualTrade.Plr2.Items) do
item.Parent = actualTrade.Plr1.Plr:FindFirstChild('Pets')
end
end
-- update said trade
TradeUpdate:FireClient(player)
TradeUpdate:FireClient(user)
trades[trade.Plr1.Plr.Name..'_'..trade.Plr2.Plr.Name] = nil
else
-- update trade that way they no it was declined
TradeUpdate:FireClient(player,actualTrade)
TradeUpdate:FireClient(user,actualTrade)
end
end
end
end)
repeat task.wait(1) until not trades[player.Name..'_'..user.Name]
connect:Disconnect() -- remove the connection if the trade is gone!
end
end
end)
tried making as many comments as possible to help read it but I still doubt anyone one has the will power to go through all of it