#Save data (DataStoreService) not working for me.

1 messages · Page 1 of 1 (latest)

opaque cedar
#

Main problem: Current data doesn't get saved after leaving.

The important bit is after the --handle leaving.
The console outputs "X" on leave but not the rest of the code.
What exaclty is the problem?

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerData")


local defaultData = {
    speed = 3, --default 16
    jumpPower = 4 --default 7.2
}


Players.PlayerAdded:Connect(function(player)
    local sucess, err = pcall(function()
        local playerData = PlayerDataStore:GetAsync(player.UserId)
        if not playerData then
            playerData = defaultData
            PlayerDataStore:SetAsync(player.UserId, playerData)
            print("New data table set for", player)
        
        else
            playerData = playerData
            print("Data loaded for", player)
            
        end
    end)
    
    player.CharacterAdded:Connect(function(character)
        local playerData = PlayerDataStore:GetAsync(player.UserId)
        local humanoid = character:WaitForChild("Humanoid")

        humanoid.WalkSpeed = playerData.speed
        humanoid.JumpPower = playerData.jumpPower
        print(humanoid, playerData)
        
    end)

end)

--handle leaving
Players.PlayerRemoving:Connect(function(player)
    local character = player.Character
    if not character then return end

    local humanoid = character:FindFirstChild("Humanoid")
    if not humanoid then return end

    local playerData = {
        speed = humanoid.WalkSpeed,
        jumpPower = humanoid.JumpPower
    }

    print("X", playerData)

    local success, err = pcall(function()
        PlayerDataStore:SetAsync(player.UserId, playerData)
    end)

    if success then
        print("Data saved for", player.Name)
    else
        warn("Failed to save data:", err)
    end
end)

--Todo fix handle save data on leave
sage geyser
#

Did you enable data store in the configuration?

opaque cedar
#

I have API services enabled for my workspace if thats what ur asking

sage geyser
#

Are you testng it in Studio?

opaque cedar
sage geyser
#

There's a setting to enable studio access to API Services - is that enabled?

#

Double check to make sure

opaque cedar
sage geyser
#

Does failed to save data/data saved for not get printed?

opaque cedar
#

it does not

sage geyser
#

Try binding to close for game

#

It's weird that it wouldn't get printed at least

opaque cedar
#

This is whats printed on leave

sage geyser
#

Maybe the game closes before it runs those lines

opaque cedar
sage geyser
#

Yeah, game maybe closing before those lines are ran or something. Or for some reason PlayerDataStore:SetAsync(player.UserId, playerData) is making it stuck - can you try putting that into a coroutine and see if it runs those print lines?

#

SetAsync should be an asynchronous call, right?

opaque cedar
#

yes

sage geyser
#

It's not asynchronous

opaque cedar
#

lol

sage geyser
#

Try wrapping that into a coroutine and try the BindToClose method I mentioned earlier and see if that changes anything

opaque cedar
#

I would expect SetAsync to be asynchronous

sage geyser
#

But it's not apparently

opaque cedar
#

alright I ll try ToBind first

#

BindToClose

sage geyser
#

Is this script in SSS by the way?

opaque cedar
#

WOW

#

bindToClose works

#

😭

sage geyser
#

Nice

opaque cedar
sage geyser
#

ServerScriptService

opaque cedar
#

it is

sage geyser
#

okay

opaque cedar
#

I am not very familiar with those shortcuts yet

#

if ur curious this works:

game:BindToClose(function()
    for _, player in pairs(Players:GetPlayers()) do
        local character = player.Character
        if not character then continue end

        local humanoid = character:FindFirstChild("Humanoid")
        if not humanoid then return end

        local playerData = {
            speed = humanoid.WalkSpeed,
            jumpPower = humanoid.JumpPower
        }

        print("X", playerData)

        local success, err = pcall(function()
            PlayerDataStore:SetAsync(player.UserId, playerData)
        end)

        if success then
            print("Data saved for", player.Name)
        else
            warn("Failed to save data:", err)
        end
    end
end)
sage geyser
#

Yeah it's okay, not that you should be lol

opaque cedar
#

pretty much the same function

sage geyser
#

Are you doing a local script?

opaque cedar
#

just a script

#

not specifically a local script

sage geyser
#

Do you need that for each loop?

#

I mean ig whatever works but if you don't need it then may be good to remove it

opaque cedar
#

remove what?

sage geyser
opaque cedar
#

have what tho xd

#

fill me in

sage geyser
#

It's probably good to externalize the data saving functionality into a function like

local function saveData(player)

end

And then do...

game:BindToClose(saveData)
players.PlayerRemoving:Connect(saveData)
sage geyser
# opaque cedar fill me in

Nothing, I just was wondering if you need the for each loop to loop through players but I think because the game:BindToClose() exists outside the PlayerRemoving, it's to help load that context of the player who's data we're saving in

opaque cedar
sage geyser
opaque cedar
#

but not too sure how accurate those infos were

#

so I just loop through all players to save their data

sage geyser
#

Anonymous functions are smaller functions usually that have very isolated behavior

sage geyser
opaque cedar
sage geyser
opaque cedar
#

👍

#

Thanks for the help