Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Datastore not saving data or giving error?

Asked by
CodeWon 181
3 years ago

I was making a data store and when I left the game, it was supposed to print "Data saved" but it didn't, nor did I get an error.

What did I do wrong?

local DataStoreService = game:GetService("DataStoreService")
local fcgDataStore = DataStoreService:GetDataStore("fcgDataStore")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    --Currency
    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats

    local gems = Instance.new("IntValue")
    gems.Name = "Gems"
    gems.Parent = leaderstats

    --Inventory
    local inventory = Instance.new("Folder")
    inventory.Name = "Inventory"
    inventory.Parent = player

    --Data
    local userId = "Player_"..player.UserId

    --Load data
    local data
    local success, errormessage = pcall(function()
        data = fcgDataStore:GetAsync(userId)    
    end)

    if success then
        coins.Value = data  
    end

end)

--Save data
game.Players.PlayerRemoving:Connect(function(player)
    local userId = "Player_"..player.UserId

    local data = player.leaderstats.Coins.Value

    local success, errormessage = pcall(function()
        fcgDataStore:SetAsync(userId, data)
    end)

    if success then
        print("Data saved")
    else
        print("Error saving data")
        warn(errormessage)
    end

end)

1 answer

Log in to vote
1
Answered by
MattVSNNL 620 Moderation Voter
3 years ago
Edited 3 years ago

Try this

-- Adding variables like the data store service, myData store is your data store, Players left is to detect if there's amount of players to detect when the server can shutdown
local dataStoreService = game:GetService("DataStoreService")
local myDataStore = dataStoreService:GetDataStore("RobloxDataStore")
local playersLeft = 0

game.Players.PlayerAdded:Connect(function(player)

    -- Everytime a player joins it add 1 more to players left

    playersLeft = playersLeft + 1

    -- Adding money to the play
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local money = Instance.new("IntValue")
    money.Name = "Money"
    money.Value = 0
    money.Parent = leaderstats

    -- This adds data into the player data with userid

    local PlayerUserId = player.UserId

    -- Making a blank variable to store data

    local player_data

    -- Checks if its successfull or else you get an error message
    -- Makes a pcall so if theres an error it won't break the script

    local success, errorMessage = pcall(function()

        -- Sets player_data as players userid in your data store

        player_data = myDataStore:GetAsync(PlayerUserId)
    end)

    -- If its successfull then the money will be what we saved

    if success then
        money.Value = player_data.Money
    end
end)

-- Makes a bindable event, Used to tell the script to do something when the game is shutting down

local bindableEvent = Instance.new("BindableEvent")

-- Fires what a player leaves the game

game.Players.PlayerRemoving:Connect(function(player)

    -- Gets the userid again

    local playerUserId = player.UserId

    -- The player_data you can save money is your money value

    local player_data = {
        Money = player.leaderstats.Money.Value
    }

    -- When a player leaves it will set your money to money in player data at amount of money you have

    local success, errorMessage = pcall(function()
        myDataStore:SetAsync(playerUserId, player_data)

        -- Removes from players left

        playersLeft = playersLeft - 1

        -- Fires the bindable event

        bindableEvent:Fire()
    end)

    -- If it was successfull then it prints saved to tell you that it was successfull else it tell you what the problem is

    if success then
        print("Saved!")
    else
        warn(errorMessage)
    end
end)

-- If the players left is above 0 then the server is gonna wait until everyones data was saved and then it can shut down

if playersLeft > 0 then
    bindableEvent.Event:Wait()
end

To add more leader stats add this under money

    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Value = 0
    coins.Parent = leaderstats

To load it add this to the success in players added

coins.Value = player_data.Coins

To save it add this in the player_data table in playerRemoving

Coins = player.leaderstats.Coins.Value
0
How can I add more leaderstats and still make it save? CodeWon 181 — 3y
0
I edited it and said where and how and an example MattVSNNL 620 — 3y
Ad

Answer this question