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

Saving System saying it wont save the data?

Asked by 4 years ago
Edited 4 years ago

Can someone please help, the error is on line 36 where it say: local data = {plr.leaderstats.Shirts, plr.leaderstats.Money.Value} Can someone please help me fix this error

local DS = game:GetService("DataStoreService"):GetDataStore("SaveData")
game.Players.PlayerAdded:Connect(function(plr)
    wait()
    local stats = Instance.new("Folder")
    stats.Name = "leaderstats"
    stats.Parent = plr

    local shirts = Instance.new("IntValue")
    shirts.Name = "Shirts"
    shirts.Value = 0
    shirts.Parent = stats

    local cash = Instance.new("IntValue")
    cash.Name = "Money"
    cash.Value = 0
    cash.Parent = stats

    local plrkey = "id_"..plr.userId
    local save1 = plr.leaderstats.Shirts
    local save2 = plr.leaderstats.Money

    local GetSaved
    local success, failed = pcall(function()
        GetSaved = DS:GetAsync(plr.UserId)
    end)
    if success then
        save1.Value = GetSaved[1]
        save2.Value = GetSaved[2]
        print(plr.Name.." Successfully recived the data!")
    else
        print("There was an error while getting data!")
        warn(failed)
    end
end)
game.Players.PlayerRemoving:Connect(function(plr)
    local data = {plr.leaderstats.Shirts, plr.leaderstats.Money.Value}

    local success, fail = pcall(function()
        DS:SetAsync(plr.UserId, data)
    end)

    if success then
        print(plr.Name.."'s Data was successfully saved!")
    else
        print("There was an error while saving data!")
        warn(fail)
    end
end)

The output is saying:
There was an error while saving data! 19:03:54.803 - 104: Cannot store Array in data store. Data stores can only accept valid UTF-8 characters.

0
You need a dictionary Ziffixture 6913 — 4y

1 answer

Log in to vote
0
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

The reason it didn't work was because with datastores you cant store an array in a datastore Datastores only accept numbers, string etc

 ds = game:GetService("DataStoreService"):GetDataStore("Points") -- GetDataStore provides a key to save everything under.  Can be anything.

game.Players.PlayerAdded:connect(function(player)
    local leaderstats = Instance.new("IntValue",player) -- Creating the leaderboard
    leaderstats.Name = "leaderstats"
    local points = Instance.new("IntValue",player)
    points.Name = "Points"
    points.Value = ds:GetAsync(player.Name) or 0 -- Here we look for the players name under "Points".  DataStores act like a table, and if the players name doesn't yet exist, 

local cash = Instance.new("IntValue")
    cash.Name = "Money"
    cash.Value = 0
    cash.Parent = player


end)

game.Players.PlayerRemoving:connect(function(player)
    ds:UpdateAsync(pplayer.Name, function(oldValue) return p.leaderstats.Points.Value end) -- This can be confusing.  The first argument, p.Name, is the name of the key inside the Points DataStore.  The value you update it with has to be a function, so function(oldValue) return value end is the recommended way to go.  The argument of the function is what the value used to be
end)

game.OnClose = function() -- This part is recommended to ensure the game saves when the last player leaves the server before it shuts down!
    for i,v in pairs(game.Players:GetChildren()) do
        ds:UpdateAsync(v.Name, function(oldValue) return v.leaderstats.Points.Value end)
    end
    wait(1)
end)
0
But also instead of using a players name u can use their user id because they might change their name in the future. Nifemiplayz 32 — 4y
0
Ik JesseSong 3916 — 4y
Ad

Answer this question