What’s up people. Here is a code I wrote out of boredom that could be useful for some people.
Real Currency is partly soft-coded module that handles currency transactions from the same server to different servers. There also is a feature to log the transactions in a datastore. There are comments that guides you what to change in the code. Enjoy!
local msgs = game:GetService("MessagingService")
local datastore = game:GetService("DataStoreService")
local plrs = game:GetService("Players")
local RC = {}
-- // info // --
-- This script assumes each player has a folder instance inside of their player instance and a number value inside of that folder. If your currency system is-
-- OOP'd then you'd have to modify the code. Good luck!
-- // info // --
local paths = {
["NotificationRemote"] = game.ReplicatedStorage.Notif -- The remote event that'll handle the notification
}
local config = {
["CurrencyName"] = "Cash", -- Name of the NumberValue that's inside of player data folder
["PlayerDataFolderName"] = "leaderstats", -- Name of the folder that holds the player data
["LogTransactions"] = true, -- Logs transactions in a datastore
["TransactionDatastoreKeyname"] = "Transactions", -- Datastore key
["SendNotification"] = false, -- Sends notification to the receiver (You need a local script that handles the notifications! SEE "paths")
["Listener"] = true, -- Listener of the messaging service. Do not interact if you don't know what you're doing
["Debug"] = false -- Debug
}
local transactionstore = datastore:GetDataStore(config.TransactionDatastoreKeyname)
function RC.enableListener()
if config.Listener then
local success, err = pcall(function()
if config.Debug then
print("Listener Enabled!")
end
msgs:SubscribeAsync("Transaction", function(message)
RC.processTransaction(message)
end)
end)
end
end
function RC.processTransaction(msg)
local playerToReceive = msg.Data.ToSend
local Sender = msg.Data.Sender
if plrs:FindFirstChild(playerToReceive) then
local plr = plrs:FindFirstChild(playerToReceive)
local plrFolder = plr:FindFirstChild(config.PlayerDataFolderName)
local plrCurrency = plrFolder:FindFirstChild(config.CurrencyName)
if plrCurrency then
if config.SendNotification and paths.NotificationRemote then
paths.NotificationRemote:FireClient(plr)
end
if config.LogTransactions and transactionstore then
local Receipt = tostring(Sender.." sent "..msg.Data.Amount.." "..config.CurrencyName.." to "..playerToReceive)
transactionstore:SetAsync(Sender, Receipt); transactionstore:SetAsync(playerToReceive, Receipt)
end
plrCurrency.Value += msg.Data.Amount
end
end
end
function RC.sendTransaction(Player1Name: string, Player2Name: string, Amount: NumberValue)
local playerObject = plrs:FindFirstChild(Player1Name)
local playerFolder = playerObject:FindFirstChild(config.PlayerDataFolderName)
local playerCurrency = playerFolder:FindFirstChild(config.CurrencyName)
if playerObject and playerFolder and playerCurrency and playerCurrency.Value >= Amount then
playerCurrency.Value -= Amount
local msg = {
["ToSend"] = Player2Name,
["Sender"] = Player1Name,
["Amount"] = Amount
}
msgs:PublishAsync("Transaction", msg)
return true
else
return false
end
end
return RC
If you need help with anything, let me know in the replies!