What is this open sourced code?
This game is sort of a real time trade exchange (Fits Under ToS). You buy virtual stocks (not irl) for virtual money inside Roblox. YOU WILL NOT GET REAL STOCKS FROM THIS GAME OR ANYTHING
How did I come up with this?
Pretty while ago, I was thinking of creating a game that could do in game not real stock exchange. I came up with this, which I have open sourced as of now.
What did I use?
I of course used HTTP service as we need to get IRL stock prices, This is the API I used
How does this work?
As I said, this uses the API to get the info:
local httpsService = game:GetService('HttpService')
--------
local currentStockInMarket = {"AAPL","MSFT","TSLA","AMC","SNDL","F","NIO","AMZN","DIS","RBLX"}
-- These are the current stocks that will be sold in game
local stockTable = currentStockInMarket -- for some reason
---------
for _,w in pairs(currentStockInMarket) do
local creatingStock = Instance.new('NumberValue') -- creates a stock in server storage
creatingStock.Name = w
creatingStock.Value = 0
creatingStock.Parent = script.Parent.Stocks
end
local currentAmout ='c'
local function yesW() -- updates it using API
for _,w in pairs(stockTable) do
local url = 'https://finnhub.io/api/v1/quote?symbol='..w..'&token=c4c32haad3idvvrv8f5g' -- LINK
local Data = httpsService:GetAsync(url)
local jsonTable = httpsService:JSONDecode(Data)
local stocks = script.Parent.Stocks
local currentStock = stocks:FindFirstChild(w)
if currentStock then
currentStock.Value = jsonTable[currentAmout]
else
warn('ERROR!')
warn(w)
warn(jsonTable[currentAmout])
end
local divided = #stockTable/500
local dividedTwo = divided*60
local waitingTime = dividedTwo/#stockTable
wait(waitingTime+0.005)
end
end
yesW()
wait(60)
while true do
yesW()
wait(60)
end -- this is basically a loop to make sure we have the right amount, you can also get new amount once every purchase is made but that wont be right for display
This is the stock transaction history log which keeps track (I wont comment this):
local repStorage = game:GetService('ReplicatedStorage')
local events = repStorage:WaitForChild('events')
local trasnaction = events:WaitForChild('buyAndsellStocks')
local httpService = game:GetService('HttpService')
local stocksYes = script.Parent.Stocks
local function scan(player)
local playerFolder = player:WaitForChild('playerFolder')
local moneyFolder = playerFolder:WaitForChild('moneyFolder')
local Money = moneyFolder:WaitForChild('Money')
local stockFolder = playerFolder:WaitForChild('stockFolder')
for _,w in pairs(stockFolder:GetChildren()) do
if w.Value <=0 then
w:Destroy()
end
end
end
local function makeTransaction(stockTransactions,yourStock,amount,cost,decision)
local characterStats = {
Stock = yourStock.Name,
Amount = amount,
Date = os.date(),
Decision = decision,
Cost = math.floor(cost+0.00005)
}
local encoded = httpService:JSONEncode(characterStats)
local transaction = Instance.new('StringValue')
transaction.Name = yourStock.Name
transaction.Parent = stockTransactions
transaction.Value = tostring(encoded)
end
local function transaction(player,tableOfContent,stockName,amount,buying)
local playerFolder = player:WaitForChild('playerFolder')
local moneyFolder = playerFolder:WaitForChild('moneyFolder')
local Money = moneyFolder:WaitForChild('Money')
local stockFolder = playerFolder:WaitForChild('stockFolder')
local stockTransactions = playerFolder:WaitForChild('stockTransactions')
local decision
if stockFolder:FindFirstChild(stockName) then
if buying == true then
local yourStock = stockFolder:FindFirstChild(stockName)
if tableOfContent[stockName]*amount<=Money.Value and tableOfContent[stockName]*amount>0 then
yourStock.Value = yourStock.Value + amount
local yesCost = (stocksYes:FindFirstChild(stockName).Value*amount)
Money.Value = Money.Value -yesCost
makeTransaction(stockTransactions,yourStock,amount,yesCost,"Bought")
decision = 1
scan(player)
else
decision = 3
scan(player)
end
elseif buying == false then
local yourStock = stockFolder:FindFirstChild(stockName)
if amount<=yourStock.Value and amount>0 then
local yesCost = (stocksYes:FindFirstChild(stockName).Value*amount)
yourStock.Value = yourStock.Value - amount
Money.Value = Money.Value +yesCost
makeTransaction(stockTransactions,yourStock,amount,yesCost,"Sold")
decision = 2
scan(player)
else
scan(player)
decision = 3
scan(player)
end
end
else
if buying == true then
local yourStock = stockFolder:FindFirstChild(stockName)
if tableOfContent[stockName]*amount<=Money.Value and tableOfContent[stockName]*amount>0 then
local newStock = Instance.new('NumberValue')
newStock.Name = stockName
newStock.Parent = stockFolder
newStock.Value = amount
local yesCost = (stocksYes:FindFirstChild(stockName).Value*amount)
Money.Value = Money.Value - yesCost
makeTransaction(stockTransactions,newStock,amount,yesCost,"Bought")
decision = 1
scan(player)
else
scan(player)
decision = 3
end
elseif buying == false then
scan(player)
decision = 3
end
end
return decision
end
trasnaction.OnServerInvoke = transaction
At last here is the code for the client info grab:
local repStorage = game:GetService('ReplicatedStorage')
local events = repStorage:WaitForChild('events')
local getStocks = events:WaitForChild('getStocks')
local function onGetStocks()
local stocksTable = {}
local stocks = {}
for _,i in pairs(script.Parent.Stocks:GetChildren()) do
stocksTable[i.Name] = i.Value
table.insert(stocks,i.Name)
end
return stocksTable,stocks
end
getStocks.OnServerInvoke = onGetStocks
Stuff
I won’t be going over the UI and the client side. It is very long but not complicated, instead you can take a look at it yourself
If you have any issues feel free to dm me at uniquedummy#3509
I am also available for small commissions atm
This code was written a while ago, I planned on creating a game with this but I can’t as I am working on another cool game so instead of just like rewriting it and making it better, I just open sourced it for people to make it better than I can, If you think some part of the code is just useless and can be optimized please keep this in note