So I’m having an issue developing a system similar to a cryptocurrency.
What needs to happen is that every 30 minutes, the price of the currency randomly changes.
The issue is I don’t know how to make this happen globally, and then if every server shuts down, how to make it save until another starts.
Currently I just have a module of the stats for each currency and a module that just has DataStoreService, MessagingService, and HTTPService imported.
The module contains each currencies name,baseprice, commission (how much is lost upon selling), and the max it can change every 30 minutes
I know it would involve datastore and cross server messaging, I just have never worked with messagingservice.
I have all my code below
MainModule
local DataStoreService = game:GetService("DataStoreService")
local MessagingService = game:GetService("MessagingService")
local HTTPService = game:GetService("HttpService")
local module = {}
return module
Is there any way I can just have one server change it than synchronize the price to all other servers?
I don’t want people to be able to swap servers and sell for a different price.
Can you just use the real time from the real world?
That would be consistent across all servers in the same time zone:
local function formatTime(use12Hour, t )
local t = t or tick()
local hours = math.floor(t / 3600) % 24
local mins = math.floor(t / 60) % 60
local secs = t % 60
local isPm = hours > 12
local amOrPm = isPm and "pm" or "am"
--convert to 12 hour time if we want to use 12 hour time instead of 24:
if use12Hour then
hours = isPm and hours - 12 or hours
return string.format("%d:%02d:%02d"..amOrPm, hours, mins, secs)
else
return string.format("%d:%02d:%02d ", hours, mins, secs)
end
end
print(formatTime(true))
I have found a solution, it can be improved heavily, but it works quite well. If you have any ideas to improve, please post them below. It even syncs across all servers!
Code:
local DataStoreService = game:GetService("DataStoreService")
local HTTPService = game:GetService("HttpService")
local Cryptos = require(script.KnownCryptos)
local DataStore = DataStoreService:GetDataStore("Cryptos")
local module = {}
local lasts = {}
function module.getCryptoTable()
return lasts
end
function module.init()
for i, v in Cryptos do
local last = v.BasePrice
local data
local err = pcall(function()
data = DataStore:GetAsync(v.Name)
end)
if data == nil or data == "nil" then
warn("No data for crypto \""..v.Name.."\", reverting to default value of: "..tostring(v.BasePrice)..".")
else
last = data
end
lasts[v.Name] = last
DataStore:SetAsync(v.Name, lasts[v.Name])
end
while task.wait(1) do
local t = tick()
local hours = math.floor(t / 3600) % 24
local mins = math.floor(t / 60) % 60
local secs = math.floor(t % 60)
if secs == 0 or secs == 30 then
for i, v in Cryptos do
local data
local err = pcall(function()
data = DataStore:GetAsync(v.Name)
end)
if data == "nil" or data == nil then
warn("NO DATA")
elseif data == lasts[v.Name] then
local randomPercent = math.random(-v.MaxChange, v.MaxChange) / 100
lasts[v.Name] = math.floor(lasts[v.Name] * (randomPercent+1))
if lasts[v.Name] < 100 then
lasts[v.Name] = 100
end
DataStore:SetAsync(v.Name, lasts[v.Name])
else
lasts[v.Name] = data
end
print(v.Name..": "..tostring(lasts[v.Name]))
end
end
end
end
return module