Hey! I am currently working on a game and I need help making a coding system for currency, I want it to be able to go onto user interface, and be able to keep and store data,
(Sorry if it is not inside the right topic, couldn’t really find one that fit this)
Firstly, you will need to use datastores to save the currency.Tutorial on datastores
If you want this data to be on a screen gui then you will need to transfer the data with a remote event. (allows server and client to communicate through scripts)
Now that you have the data, you can set the text to the currency value.
script in serverscriptservice
local dss = game:GetService("DataStoreService")
local CurrencyVal = dss:GetDataStore("CurrencyValue")
local rs = game:GetService("ReplicatedStorage")
game.Players.PlayerAdded:Connect(function(player)
local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player
local currency = Instance.new("IntValue") --this is the currency
currency.Name = "currency"
currency.Parent = leaderstats
local plrKey = "id_" .. player.UserId
local saved = CurrencyVal:GetAsync(plrKey)
local save = player.leaderstats.currency
if saved then
save.Value = saved[1]
rs:WaitForChild("Currency"):FireAllClients(saved[1])
end
end)
game.Players.PlayerRemoving:Connect(function(player)
CurrencyVal:SetAsync("id_"..player.UserId,{player.leaderstats.currency.Value}) --use this whenever you give the player an amount of currency
end)
local script in screen gui
local replicatedStorage = game:GetService("ReplicatedStorage")
local CurrencyGui = script.Parent.Amount
replicatedStorage:WaitForChild("Currency").OnClientEvent:Connect(function(data)
CurrencyGui.Text = "Amount of currency: "..data
end)
this is what your screenGui should look like in starterGui
you also need a remote event in replicatedStorage called Currency
I am not experienced with NPC’s but I’m sure there’s a tutorial out there
Let me know if anything isn’t functioning correctly