#Storing Data using DataStoreService

38 messages · Page 1 of 1 (latest)

crystal ravine
#

So, I've created a script to save a value called "Dinero", Which is Money in spanish, I'm basically trying to save money into a datastore then load it back in, It seems to work except that it saves 0 instead of the 1 I've set the value to.

#
local DataStore = DataStoreService:GetDataStore("PlayerStats")
local function SaveData(Player, Stat)
    local success, errormessage = pcall(function()
        local Stat1 = Player.leaderstats[Stat]
        local UserKey = Player.UserId
        DataStore:SetAsync("Player"..UserKey.."Stat"..Stat, Stat1.Value)
    end)
    
    if success then
        print("Player "..Player.Name.."'s Data has been successfully saved, "..Player:FindFirstChild("leaderstats"):FindFirstChild(Stat).Value..", "..Stat)
    else
        print("Player"..Player.Name.."'s Data has not been saved.")
        warn(errormessage)
    end
end

local function LoadData(Player, Stat)
    local StatData
    local success, errormessage = pcall(function()
        local Stat1 = Player:FindFirstChild("leaderstats"):FindFirstChild(Stat)
        local UserKey = Player.UserId
        StatData = DataStore:GetAsync("Player"..UserKey.."Stat"..Stat)
        Stat1.Value = StatData
    end)

    if success then
        print("Player "..Player.Name.."'s Data has been successfully loaded")
    else
        warn("Player"..Player.Name.."'s Data has not been loaded.")
        warn(errormessage)
    end
end

local Players = game.Players
Players.PlayerAdded:Connect(function(Player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Parent = Player
    leaderstats.Name = "leaderstats"
    
    local CopiarDinero = script.Valores.Dinero
    local Dinero = CopiarDinero:Clone()
    Dinero.Parent = leaderstats
    LoadData(Player, "Dinero")
end)

Players.PlayerRemoving:Connect(function(Player)
    SaveData(Player, "Dinero")
end)```
elder quail
#

this is pretty basic it doesn't always save

local DataStoreService = game:GetService("DataStoreService")
local DineroData = DataStoreService:GetDataStore("Dinero")

local function SaveDinero(player, dinero)
    DineroData:SetAsync(player.userId, dinero)
end

local function LoadDinero(player)
    return DineroData:GetAsync(player.userId) or 0
end

game.Players.PlayerLeaving:Connect(function(player)
    local dinero = player.leaderstats.Dinero.Value
    SaveDinero(player, dinero)
end)

game.Players.PlayerAdded:Connect(function(player)
    local dinero = LoadDinero(player)
    player.leaderstats.Dinero.Value = dinero
end)

while true do
wait(45)
SaveDinero()
end```
#

its only been used once

charred tapir
#

Create a script using for loops to save all (more) stats inside a table, save the table and load the table

elder quail
#

ive tried that but im sending too many requests (45s)

crystal ravine
elder quail
#

I have to use a loop for backing it up incase if it doesnt save when the player leaves

#

if the backup is added, there shouldn't be any data loss

deft roverBOT
#

studio** You are now Level 1! **studio

crystal ravine
#

Really that seems like a great solution but I don't think I would apply that to my game

#

Thanks though

charred tapir
fossil adder
#

Lol but there can be a some fixes

fossil adder
charred tapir
fossil adder
charred tapir
#

What?

fossil adder
#

Wow u don’t know but I’ll show u

fossil adder
# charred tapir What?
local Data = game:GetService('DataStoreService'):GetDataStore('Save')
local function UpdateData(player)
    local ls = Instance.new('Folder',player)
    ls.Name = 'leaderstats'
    local Saving = Data:GetAsync(player.UserId)
    local MainValues = {
        {key = 'Coins',Value = 0},
        {key = 'Multi',Value = 0},
        {key = 'Rebirths',Value = 0}}
    for i,v in pairs(MainValues) do
        local Number = Instance.new('NumberValue',ls)
        Number.Name = v.key
        Number.Value = v.Value
        if Saving then
            Number.Value = Saving[Number.Name]
        end
    end
end
local function LoadData(player)
    local ls = player:FindFirstChild('leaderstats')
    local Saving = {}
    for i,v in pairs(ls:GetChildren()) do
        Saving[v.Name] = v.Value
    end
    return Saving
end
local Player = game.Players
Player.PlayerAdded:Connect(function(player)
    UpdateData(player)
end)
Player.PlayerRemoving:Connect(function(player)
    local DidSave = LoadData(player)
    Data:SetAsnyc(player.UserId,DidSave)
end)
charred tapir
#

Amd how is this better?

fossil adder
#

because u dont need to create like over 100 Instance.new

#

each time creating a new Value

charred tapir
#

Um... Yea ig, didnt even think of creating a table to be used for a loop to create instances

#

But for the OP, I think my script is a little bit easier to understand

fossil adder
#

how so urs makes it not simple

charred tapir
#

It just create a normal instance that he prob knows how to use it

fossil adder
#

well the one i have doenst need to create a lot of Instances

#

it just needs a Table and a couple of Instances to contain the Table

charred tapir
#

Yea, I get it, but for someone that is new to Lua, that might be confusing

fossil adder
charred tapir
#

You really wanna argue about it huh?

fossil adder
#

Ur the one arguing not me since people don’t do the correct thing

charred tapir
#

From my POV, you are the one who start arguing after I sent the script

fossil adder
charred tapir
#

Damn... Didn't know you are this mad, sorry

fossil adder
#

not mad its that u dont know what to do