Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Saving System saying it wont save the data?

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.

1 comment

0

You need a dictionary

1 answer

✓ Accepted answer

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)

2 comments

0

But also instead of using a players name u can use their user id because they might change their name in the future.

0

Ik

This discussion is preserved as it was. New answers, comments, and votes are closed.