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

Why does my leaderstats script in serverscriptservice not save anything?

Asked by
Nozazxe 107
3 years ago

Hello. I am trying to make a game where you just sit and get lazyness and stuff. but my leaderstats wont work. can someone tell me why?

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("Lazyness")
local ds2 = ds:GetDataStore("LPS")

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

    local folder = Instance.new("Folder", player)
    folder.Name = "leaderstats"

    local Lazyness = Instance.new("IntValue", folder)
    Lazyness.Name = "Lazyness"

    local LPS = Instance.new("IntValue", folder)
    LPS.Name = "LPS"

    Lazyness.Value = ds1:GetAsync(player.UserId) or 0
    ds1:GetAsync(player.UserId, Lazyness.Value)

    LPS.Value = ds2:GetAsync(player.UserId) or 1
    ds2:GetAsync(player.UserId, LPS.Value)

end)

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

    local Lazyness = player.leaderstats.Lazyness
    local LPS = player.leaderstats.LPS

    ds1:GetAsync(player.UserId, Lazyness.Value)

    ds2:GetAsync(player.UserId, LPS.Value)
end)

1 answer

Log in to vote
1
Answered by 3 years ago

you never setted the value in synchronization. all you did was GetASync and didnt finish it with SetASync.

local ds = game:GetService("DataStoreService")
local ds1 = ds:GetDataStore("Lazyness")
local ds2 = ds:GetDataStore("LPS")

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

    local folder = Instance.new("Folder", player)
    folder.Name = "leaderstats"

    local Lazyness = Instance.new("IntValue", folder)
    Lazyness.Name = "Lazyness"

    local LPS = Instance.new("IntValue", folder)
    LPS.Name = "LPS"

    local userid = "Player_"..player.UserId

    local LPSData
    local LazynessData
    local Success, Error = pcall(function()
        LazynessData = ds1:GetAsync(userid)
        LPSData = ds2:GetAsync(userid)
    end)

    if Success then
        Lazyness.Value = LazynessData
        LPS.Value = LPSData
    end
end)

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

    local userid = "Player_"..player.UserId

    local Lazyness = player.leaderstats.Lazyness.Value
    local LPS = player.leaderstats.LPS.Value

    local Success, ErrorMessage = pcall(function()
        ds1:SetAsync(userid, Lazyness)
        ds2:SetAsync(userid, LPS)
    end)

    if Success then
        print("Successfully saved data for: "..player.Name)
    else
        warn("Could not save data for: "..player.Name)
        wait(2)
        error(ErrorMessage)
    end
end)

You can't just get the players name in synchronization and not set it after the player got removed.

Ad

Answer this question