I wanna make a trading system but most tutorials are about trading tools but my game works on storing data through tables and strings in those tables (hopefully that makes sense). Anyways, I don’t really know how to go about this since there are so many factors to consider like how would I handle duplicates in the players owned stuff table (they’re all string values so idk how to differentiate them) or avoid duping glitches, etc. Any tutorials or posts or just advice to get me started (or maybe even some sample code jk) Any help is appreciated, thanks!
For a simple and rugged trading system, you will need a few things:
- Session locking - Session locking is a solution to dealing with the race condition between loading and saving data with DataStore API calls, when for instance, loading data is faster than the data being saved in a time frame despite saving data being first.
This ensures you will not have multiple reads/writes going at once.
- Item tracking - utilize a method for creating & serializing the item along with corresponding metadata about the item: UUID (HttpService:GenerateGuid()), CreatedBy (Player), CreatedDate (DateTime.now()).
This will give you information to help you identify this item from every other item in the game, and give you some valuable information that you can use for your own purposes.
-
Preventing Duplication - UUID checks and trade monitoring (Discord Webhooks?)
-
A safe method for trading - Both parties are able and willing to trade. Ensured by utilizing server side checks.
I recommend ProfileService to help with session locking and intuitive data storage:
https://madstudioroblox.github.io/ProfileService/
For the data tracking, I recommend using a special registry class from which every item which will be traded is extended from:
local Registry = {}
Registry.__index = Registry
local HttpService = game:GetService("HttpService")
function Registry.new(player: Player)
local self = setmetatable({}, Registry)
self.UUID = HttpService:GenerateGUID(false)
self.CreatedBy = player.Name
self.CreatedDate = tostring(DateTime.now():FormatUniversalTime("LLLL", "en-us"))
return self
end
function Registry:Destroy()
self.UUID = nil
self.CreatedBy = nil
self.CreatedDate = nil
end
return Registry
Let me know if you have any questions! Happy trading!
Hey man, for step 3, dupe prevention, or discord webhook checks, could you go into more detail on that? I’m not familiar with roblox to discord checks like that.
Sure thing!
Webhooks are simply a way to notify you of an action in a program. Follow the steps listed here to create a webhook:
https://support.discord.com/hc/en-us/articles/228383668-Intro-to-Webhooks
Here is an example of how this would look in Luau (Make sure to allow HTTP requests!)
local HttpService = game:GetService("HttpService")
local webhookUrl = "YOUR_DISCORD_WEBHOOK_URL" -- Replace this with your actual webhook URL
local data = {
["content"] = "Hello from Roblox!",
["username"] = "Roblox Webhook"
}
local jsonData = HttpService:JSONEncode(data)
local success, response = pcall(function()
return HttpService:PostAsync(webhookUrl, jsonData, Enum.HttpContentType.ApplicationJson)
end)
if success then
print("Message sent successfully!")
else
warn("Failed to send message: ", response)
end
In addition to posting to your Webhook, save each player’s transaction history in their Profile so that there is a crosscheck.
Last but not least is to implement Session Locking, which is why I recommend ProfileStore (previously ProfileService): GitHub - MadStudioRoblox/ProfileStore: Periodic DataStore saving solution with session locking
This will ensure that the player is not in multiple sessions at once, allowing them to mess with the Datastore(s).
This way, each item is tracked in multiple spots, and you can easily log, track, and look up spawned items in a dedicated discord channel (and through the datastores, just in case).
Hope this helps, good luck!