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

Datastore not loading values when exiting / rejoining?

Asked by
Phixl 11
3 years ago

still new to coding, help me on ways this can be fixed thank you!!!!

local DataStoreService = game:GetService("DataStoreService")
local DataStore = DataStoreService:GetDataStore("DataStore")

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

    local Shards = Instance.new("IntValue")
    Shards.Name = "Shards"
    Shards.Parent = Leaderstats

    local Level = Instance.new("IntValue")
    Level.Name = "Level"
    Level.Parent = Leaderstats

    local Face = Instance.new("IntValue")
    Face.Name = "Face"
    Face.Parent = Leaderstats

    local Shirts = Instance.new("IntValue")
    Shirts.Name = "Shirts"
    Shirts.Parent = Leaderstats

    local Pants = Instance.new("IntValue")
    Pants.Name = "Pants"
    Pants.Parent = Leaderstats




    -- End of Leaderstats setup 
    -- Loading the data...




    local playerUserId = "Player_"..player.UserId

    local Data
    local success, errorMessage = pcall(function()
        Data = DataStore:GetAsync(playerUserId)
    end)

    if success then
        if Data then
            Shards.Value = Data.Shards
            Level.Value = Data.Level
            Face.Value = Data.Face
            Shirts.Value = Data.Shirts
            Pants.Value = Data.Pants
        end
    end
end)




game.Players.PlayerRemoving:Connect(function(player)
    local playerUserId = "Player_"..player.UserId

    local Data = {
        Shards = player.Leaderstats.Shards.Value;
        Level = player.Leaderstats.Level.Value;
        Face = player.Leaderstats.Face.Value;
        Shirts = player.Leaderstats.Shirts.Value;
        Pants = player.Leaderstats.Pants.Value;
    }

    local success, errorMessage = pcall(function()
        wait(0.1)
        DataStore:SetAsync(playerUserId, Data)
    end)

    if success then
        print("Data has been successfully saved")
    else
        print("Data has failed to save, Error")
        warn(errorMessage)
    end

end)

2 answers

Log in to vote
0
Answered by 3 years ago

I tested your script and it's working. I'm assuming you tested your script on studio?

This is because studio server shutdowns as you leave/stop. Try using game:BindToClose() for your saving method.

local function save(player)
    local playerUserId = "Player_"..player.UserId

    local Data = {
        Shards = player.Leaderstats.Shards.Value;
        Level = player.Leaderstats.Level.Value;
        Face = player.Leaderstats.Face.Value;
        Shirts = player.Leaderstats.Shirts.Value;
        Pants = player.Leaderstats.Pants.Value;
    }

    local success, errorMessage = pcall(function()
        wait(0.1)
        DataStore:SetAsync(playerUserId, Data)
    end)

    if success then
        print("Data has been successfully saved")
    else
        print("Data has failed to save, Error")
        warn(errorMessage)
    end
end

game.Players.PlayerRemoving:Connect(function(plr)
    save(plr)
end)

game:BindToClose(function() -- runs when game closes
    for i, plr in pairs(game.Players:GetPlayers()) do
        save(plr)
    end
end)
Ad
Log in to vote
0
Answered by 3 years ago

I think the problem is that you typed leaderstats with an uppercase L

Try changing your code from "Leaderstats" to "leaderstats"

0
It's not exactly the problem. Typing "Leaderstats" just make them won't show in player list Feelings_La 399 — 3y

Answer this question