Hi guys!
How can I fix this problem?
I trying to abbreviate cash. And this isn’t working.
When the player respawn his money got lost… But the CurrencyScript has a DB.
I can’t explain it more to you guys…
Here is some pictures and the scripts:
-- MoneyUpdate [LocalScript]
local player = game.Players.LocalPlayer or game.Players.PlayerAdded:Wait()
local leaderboard = player:WaitForChild("leaderstats")
local money = leaderboard.Money
local value = script.Parent
local AbbreviateNumber = require(game.ReplicatedStorage.AbbreviateNumber)
local number = player.leaderstats.Money.Value
print(number)
script.Parent.Parent.Money2.Text = number
local text = AbbreviateNumber:Abbreviate(number)
print(text)
script.Parent.Text = text
-- AbbreviateNumber
local AbbreviateNumber = {}
local abbreviations = {
K = 4,
M = 7,
B = 10,
Q = 13
}
function AbbreviateNumber:Abbreviate(number)
local text = tostring(math.floor(number))
local chosenAbbreviation
for abbreviation, digits in pairs(abbreviations) do
if #text >= digits and #text < (digits + 3) then
chosenAbbreviation = abbreviation
break
end
end
if chosenAbbreviation then
local digits = abbreviations[chosenAbbreviation]
local rounded = math.floor(number / 10 ^ (digits - 2)) * 10 ^ (digits - 2)
text = "$" .. string.format("%.1f", rounded / 10 ^ (digits - 1)) .. chosenAbbreviation
else
text = "$" .. number
end
return text
end
return AbbreviateNumber
-- CurrencyServer
local currencyName = "Money"
local DataStore = game:GetService("DataStoreService"):GetDataStore("TestDataStore")
game.Players.PlayerAdded:Connect(function(player)
local folder = Instance.new("Folder")
folder.Name = "leaderstats"
folder.Parent = player
local currency = Instance.new("IntValue")
currency.Name = currencyName
currency.Parent = folder
local ID = currencyName.."-"..player.UserId
local savedData = nil
pcall(function()
savedData = DataStore:GetAsync(ID)
end)
if savedData ~= nil then
currency.Value = savedData
else
-- New player
currency.Value = 100
print("New player to the game")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local ID = currencyName.."-"..player.UserId
DataStore:SetAsync(ID,player.leaderstats[currencyName].Value)
end)
game:BindToClose(function()
-- When game is ready to shutdown
for i, player in pairs(game.Players:GetPlayers()) do
if player then
player:Kick("This game is shutting down")
end
end
wait(5)
end)