-
What do you want to achieve? I want to make a datastore that stores multiple values
-
What is the issue? The second value (currencyMult) returns 0 no matter what.
-
What solutions have you tried so far? I’ve tried changing the format of data, how it’s saved, and how its fetched.
local DSS = game:GetService("DataStoreService")
local currencyDS = DSS:GetDataStore("currencyDS")
game.Players.PlayerAdded:Connect(function(plr)
-- stuff to save
local currency = Instance.new("Folder")
currency.Name = "Currency"
currency.Parent = plr
local cash = Instance.new("NumberValue")
cash.Name = "Cash"
cash.Parent = currency
local currencyMult = Instance.new("IntValue")
currencyMult.Name = "CurrencyMult"
currencyMult.Parent = currency
currencyMult.Value = 1
-- sets data into a table to save multiple values, makes a player UID a key for datastores
local playerUID = "player_" .. plr.UserId
local data = {}
-- fetches datastore data and saves it
local success, err = pcall(function()
data = currencyDS:GetAsync(playerUID)
end)
if success then
print("Success")
if data then
print("data collected")
cash.Value = data[1]
currencyMult.Value = data[2]
else
--without a datastore it will make the player not have any cash
cash.Value = 0
currencyMult.Value = 1
end
else
warn(err)
end
end)
game.Players.PlayerRemoving:Connect(function(plr)
local currency = plr.Currency
local cash = currency.Cash
local currencyMult = currency.CurrencyMult
local data = {cash.Value, currencyMult.Value}
local playerUID = "player_" .. plr.UserId
-- saves data when leaving
local success, err = pcall(function()
currencyDS:SetAsync(playerUID, data)
end)
if success then
print("data saved")
else
warn(err)
end
end)
Please help and tell me why it returns 0!