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

Trying to make a DataStore, but it doesn't appear to save the data. What is wrong with my script?

Asked by 3 years ago

I have it so that when a player leaves it creates a table with their data, and when they join it retrieves that data, but if they have no data it sets a default value. But it doesn't seem to save the data because when you join, it just sets it to the default value no matter what

script:


local Players = game:GetService("Players") local DataStoreService = game:GetService("DataStoreService") local PlayerData = DataStoreService:GetDataStore("TestDataV1") Players.PlayerAdded:Connect(function(player) local Data; local DataFetchSuccess,ErrorMessage = pcall(function() Data = PlayerData:GetAsync(player.UserId) end) if not DataFetchSuccess then player:Kick("Unable to load your data, please rejoin.") return end local Stats = Instance.new("Folder") Stats.Name = "Stats" local Cash = Instance.new("IntValue", Stats) Cash.Name = "Cash" local Stars = Instance.new("IntValue", Stats) Stars.Name = "Stars" Stats.Parent = player if Data then Cash.Value = Data.Cash Stars.Value = Data.Stars else Cash.Value = 100 Stars.Value = 5 end end) Players.PlayerRemoving:Connect(function(player) local Data = { } Data.Cash = player.Stats.Cash.Value Data.Stars = player.Stats.Stars.Value local DataWriteSuccess,ErrorMessage = pcall(function() PlayerData:SetAsync(player.UserId, Data) end) if not DataWriteSuccess then warn("Couldn't save data of "..player.Name) end end)
0
I have the same problem as well. I don't know what's happening. Dovydas1118 1495 — 3y

2 answers

Log in to vote
0
Answered by 3 years ago

You need to use game:BindToClose alongside PlayerRemoving. The problem with only having PlayerRemoving is that the server already shuts down whilst your data is still in the middle of saving. BindToClose will prevent this because the function must have run before the game server shuts down.

0
This still doesn't help. Even though I'm not the original person who answered the question, I tried this option and it still won't make it work. Dovydas1118 1495 — 3y
0
It actually worked for me, post your script xJoshRyan 23 — 3y
0
I actually can't because this is a separate question. I might post mine later. Dovydas1118 1495 — 3y
Ad
Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

You forgot to use DataStore:SetAsync if the player is new, so there is no data made. Instead, you should try this:

local Players = game:GetService("Players")

local DataStoreService = game:GetService("DataStoreService")
local PlayerData = DataStoreService:GetDataStore("TestDataV1")


Players.PlayerAdded:Connect(function(player)
    local Data;

    local DataFetchSuccess,ErrorMessage = pcall(function()
        Data = PlayerData:GetAsync(player.UserId)
    end)

    if not DataFetchSuccess then
        player:Kick("Unable to load your data, please rejoin.")
        return
    end

    local Stats = Instance.new("Folder")
    Stats.Name = "leaderstats"

    local Cash = Instance.new("IntValue", Stats)
    Cash.Name = "Cash"

    local Stars = Instance.new("IntValue", Stats)
    Stars.Name = "Stars"

    Stats.Parent = player

    if Data then
        Cash.Value = Data.Cash
        Stars.Value = Data.Stars
    else -- Data doesn't exist
        local DefaultValues = {
        cash = 100,
        stars = 5
        }
        PlayerData:SetAsync(player.UserId, DefaultValues) -- set data store to default values
        Cash.Value = DefaultValues.cash -- set leaderstats to default values
        Stars.Value = DefaultValues.stars
    end
end)


Players.PlayerRemoving:Connect(function(player)
    local Data = {

    }
    Data.Cash = player.leaderstats.Cash.Value
    Data.Stars = player.leaderstats.Stars.Value

    local DataWriteSuccess,ErrorMessage = pcall(function()
        PlayerData:SetAsync(player.UserId, Data)
    end)

    if not DataWriteSuccess then
        warn("Couldn't save data of "..player.Name)
    end
end)

I highly recommend the "Datastore Editor" plugin by Crazyman32, it really helped me do this.

Answer this question