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

Why my second value from leaderstats is not saving?

Asked by
MpAlex 16
2 years ago

I want to save 2 int values, the first one is saving, everything is fine but when i try to save the second one is not saving at all.

I looked on google for a fix, i modified the whole script as i found online and is still not working

Here is the code:

local DataStoreService = game:GetService("DataStoreService")
local KingPointsStore = DataStoreService:GetDataStore("KingPointsStore")
local tutorialStore = DataStoreService:GetDataStore('tutorialStore')

game.Players.PlayerAdded:Connect(function(player)

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

    local King = Instance.new("IntValue")
    King.Name = "KingPoints"
    King.Parent = leaderstats

    local tutorial = Instance.new('IntValue')
    tutorial.Name = 'Tutorial'
    tutorial.Parent = leaderstats
    tutorial.Value = 0


    local UserId = player.UserId

    local kingData
    local tutorialData

    local success, errormessage = pcall(function()
        kingData = KingPointsStore:GetAsync(UserId)
        tutorialData = tutorialStore:GetAsync(UserId)
    end)

    if success then
        King.Value = kingData
        tutorial.Value = tutorialData
    end
end)



game.Players.PlayerRemoving:Connect(function(player)
    local UserId = player.UserId
    local kingData = player.Stats.KingPoints.Value
    local tutorialData = player.Stats.Tutorial.Value

    KingPointsStore:SetAsync(UserId, kingData)
    tutorialStore:SetAsync(UserId, tutorialData)
    print('Data saved')
end)

1 answer

Log in to vote
0
Answered by 2 years ago

Hello! Try this code instead:

local DSS = game:GetService("DataStoreService")
local Store = DSS:GetDataStore("StoreValues")

game.Players.PlayerAdded:Connect(function(plr)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = plr

    local King = Instance.new("IntValue")
    King.Name = "KingPoints"
    King.Parent = leaderstats

    local Tutorial = Instance.new("IntValue")
    Tutorial.Name = 'Tutorial'
    Tutorial.Parent = leaderstats

    local data
    local success,err = pcall(function()
        data = Store:GetAsync(plr.UserId)
    end)

    if not success then
        error("Could not get player data.")
    else
        if data ~= nil then
            King.Value = data[1]
            Tutorial.Value = data[2]
        else
            warn("No data found.")
        end
    end

end)

game.Players.PlayerRemoving:Connect(function(plr)
    local data = {}

    data[1] = plr.leaderstats.KingPoints.Value
    data[2] = plr.leaderstats.Tutorial.Value


    Store:SetAsync(plr.UserId,data)
end) 

If you want an explanation, you can ask and you shall recieve.

0
is still not working, could it be because i'm changing the "Tutorial" value from a local script? MpAlex 16 — 2y
0
Yes, that's what is causing it. The server can't read your client. You can use RemoteEvent to change the values from a server. Good luck! LikeableEmmec 470 — 2y
0
indeed it worked now, thank you for the help MpAlex 16 — 2y
Ad

Answer this question