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

DataStore not saving data?

Asked by 4 years ago

I'm trying to make a skin system that saves the values of your skins. When testing, however, my script didn't work, and I kept getting the error print. Here is my script:

local DSS = game:GetService("DataStoreService")
local SkinDataStore = DSS:GetDataStore("SkinStore")

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

    local RoyaleMtar = Instance.new("StringValue")
    RoyaleMtar.Name = "RoyaleMtar"
    RoyaleMtar.Parent = Skins
    local DesertPGH = Instance.new("StringValue")
    DesertPGH.Name = "DesertPGH"
    DesertPGH.Parent = Skins

    local Id = "Player_"..player.UserId
    local SkinData
    local success, errormessage = pcall(function()
        SkinData = SkinDataStore:SetAsync(Id)
    end)
    if success then
        RoyaleMtar.Value = SkinData
        DesertPGH.Value = SkinData
        print("hi" )
    else
        print("error")
        warn(errormessage)
    end
end)
game.Players.PlayerRemoving:Connect(function(player)
    local Id = "Player_"..player.UserId
    local data = player.Skins.RoyaleMtar.Value
    local data1 = player.Skins.DesertPGH.Value

    local success, errormessage = pcall(function()
        SkinDataStore:SetAsync(Id, data, data1)
    end)
    if success then
        print("Skins Saved")
    else
        print("Skins failed to save")
        warn(errormessage)
    end
end)

1 answer

Log in to vote
1
Answered by
Filipalla 504 Moderation Voter
4 years ago
Edited 4 years ago

You seem to have accidentally put SetAsync on line 20 where you actually want to use GetAsync. Also, you are using SetAsync incorrectly, I would recommend using a dictionary to store multiple things

like so

SkinDataStore:SetAsync(Id, {
    RoyaleMtar = data,
    DesertPGH = data1
})

and retrieve the data like this

local SkinData
local success, errormessage = pcall(function()
    SkinData = SkinDataStore:GetAsync(Id)
end)
if success then
    RoyaleMtar.Value = SkinData.RoyaleMtar
    DesertPGH.Value = SkinData.DesertPGH
    print("hi" )
else
    print("error")
    warn(errormessage)
end

Wiki:

Dictionaries

SetAsync

GetAsync

Don't forget to mark my answer as the solution and upvote it if it answered your question :)

1
ah! ty, ill test it. hihowareya115 53 — 4y
Ad

Answer this question