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

Datastores won't load / save data. Help?

Asked by
krez99 0
5 years ago

Hello,

I have made a trail shop system. It checks whether player has purchased trails before (bool values same names as trails) https://imgur.com/5zYzqvs

This is a code which runs when the player joins/leaves to load/save data.

local DataStore = game:GetService("DataStoreService"):GetDataStore("Guns")
local BaseStats = game.ServerStorage.BaseStats
local DataVersion = 1

game.Players.PlayerAdded:connect(function(play)
    local stats = game.ServerStorage.BaseStats:Clone()
    stats.Name = play.Name
    stats.Parent = game.ServerStorage.Stats


local Key = play.UserId.."Guns"..DataVersion
local gunTable
local success = pcall(function()
    gunTable = DataStore:GetAsync(Key)
end)
if not success then
print("Failed to retrieve data")
else
if gunTable~=nil then
for i,v in pairs(gunTable) do
if stats:findFirstChild(v) then
stats[v].Value = true

end
end
end

end

    play.CharacterAdded:connect(function(char)
        game.ReplicatedStorage.OnRespawn:FireClient(play)
    end)

end)

game.Players.PlayerRemoving:connect(function(play)
    if game.ServerStorage.Stats:FindFirstChild(play.Name) then

      local Key = play.UserId.."Guns"..DataVersion
    local gunTable = {}

        for i,v in pairs(game.ServerStorage.Stats:FindFirstChild(play.Name):GetChildren()) do
    if v.Value == true then
    table.insert(gunTable,v)
    end
    end
local success = pcall(function()
    DataStore:SetAsync(Key,gunTable)
end)
if not success then
print("Data failed to save")
end

        game.ServerStorage.Stats:FindFirstChild(play.Name):Destroy()
    end
end)

The issue I'm having is: https://imgur.com/pNKF1AS

The output is always "Data failed to save"

I wonder whether anyone has any fixes to this code, looking forward to the responses. thank you

Answer this question