Why isn't my player data save working?

So I was trying to do a player data save, but it was not working. This is just a test world, so this is not important, but I would like to know for future reference. When I open it from studio, it says success (this is through the print, and I am not trying to test it through roblox studio, I publish data test games with api access and play them though roblox) but the data never saves, here is my script

2 Likes

What I’d do, is also encase your data variable in a pcall when calling the GetAsync function so that you can check it as well:

local data 

local success, whoops = pcall(function()
    data = playerData:GetAsync(PlayerUserId)
end)

if success and data ~= nil then
    currency.Value = data
else
    print("An error has occured attempting to get the data:", whoops)
end
1 Like

i just tried it, and although i was doing it through studio, the console did not do anything when i rejoined the game, i will try to play the game through a test server now i guess

Also question, why do you have the playerUserId variable inside your pcall when trying the save the data? You could just leave it outside the function

ok, but do you think that is part of the issue

No but it is something you should change cause it feels a bit off to have

Also do make sure that you have API Services turned on

Ok so you named your folder wrong on line 7. Try this on line 7:

folder.Name = leaderstats

Roblox just says you have to name your folder leaderstats or it won’t work. Idk y.

1 Like

but cName was a variable that was called “leaderstats”

That’s a variable attemption, change that to “leaderstats” so it’s a string lol
Good eye though

1 Like

On your onPlayerExit function, try this:

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

    local success, whoops = pcall(function()
        playerData:SetAsync(playerUserId, player.leaderstats.coins.Value)
    end)

    if success then
        print("Saved data successfully!")
    else
        print("An error has occured! Error: ", whoops)
    end
end

Ok try naming the folder variable to leaderstats. And remove the cName variable.

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

    -- And So On
end

Also make sure to publish to roblox and play the actual game and not in studio.

i already did all of that, so do not worry

oh, and something else weird, sometimes when i publish a game and turn api on, it gives me a message in the studio console that says Could not publish configuration settings.

forgot to mention, i also get a message in the console saying " HTTP 400 (Bad Request)"

Could you try creating a new place & change the API Services that way? You should be able to find it in your Game Settings

i am going to try to make a new game and copy the script

Ok, I’ll try to redo all this then:

local dataStore = game:GetService("DataStoreService")
local playerData = dataStore:GetDataStore("playerData")


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

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


    local coins = Instance.new("IntValue")
    coins.Name = "Coins"
    coins.Parent = leaderstats

    local ID = "Player: " .. player.UserId
    local data = playerData:GetAsync(ID)

    if data then
        coins.Value = data
    else
        coins.Value = 0
    end
end)


game.Players.PlayerRemoving:Connect(function(player)
    local ID = "Player: " .. player.UserId
    local success,err = pcall(function()
        playerData:SetAsync(ID, player.leaderstats.Coins.Value)
    end)

    if success then
        print("Successfully saved data")
    else
        warn(err)
    end
end)

i will try to copy this script

i just tried the script, and it does not work for some reason

Any errors. And use print statements to see what’s working and what’s not.