Roblox wont save datastore properly on studio or in-game

185 views Asked by At

Roblox's datastore service wont setasync properly but it will always load perfectly. I dont understand why this is happening and it wont save in-game or on studio. The only way for me to save data reliably is to use the studio plugin "Datastore Editor".

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

local function onPlayerJoin(player)
    local PlayerStats = script.PlayerStats:Clone()
    PlayerStats.Parent = player
    
    local playerUserId = 'Player_'..player.UserId
    local wins = playerData:GetAsync(playerUserId.."-Wins")
    local jump = playerData:GetAsync(playerUserId.."-Jump")
    print("Player"..player.Name.." has "..wins.." wins and "..jump.." jump power")
    
    if wins then
        PlayerStats.Wins.Value = wins
        print("Loaded wins for "..playerUserId.." ("..player.Name..")")
    else
        print("Failed to load wins for "..playerUserId.." ("..player.Name..")")
    end
    
    if jump then
        PlayerStats.Jump.Value = jump
        print("Loaded jump for "..playerUserId.." ("..player.Name..")")
    else
        print("Failed to load jump for "..playerUserId.." ("..player.Name..")")
    end
    
    while wait(10) do
        local success, err = pcall(function()
            local playerUserId = "Player_"..player.UserId
            playerData:SetAsync(playerUserId.."-Wins", player.PlayerStats.Wins.Value)
            playerData:SetAsync(playerUserId.."-Jump", player.PlayerStats.Jump.Value)
            print("Saved data for "..playerUserId.." ("..player.Name..")")
        end)

        if not success then
            warn("Failed to save data for "..player.UserId.." ("..player.Name..")")
        end
    end
end

local function onPlayerExit(player)
    local success, err = pcall(function()
        local playerUserId = "Player_"..player.UserId
        playerData:SetAsync(playerUserId.."-Wins", player.PlayerStats.Wins.Value)
        playerData:SetAsync(playerUserId.."-Jump", player.PlayerStats.Jump.Value)
        print("Saved data for "..playerUserId.." ("..player.Name..")")
    end)
    
    if not success then
        warn("Failed to save data for "..player.UserId.." ("..player.Name..")")
    end
end

game.Players.PlayerAdded:Connect(onPlayerJoin)
game.Players.PlayerRemoving:Connect(onPlayerExit)

My leaderstats creating code may look different to usual because i just clone the folder with everything inside from the script.

I expect it to work properly saving and loading data but it only loads and i dont understand why

1

There are 1 answers

1
Arkinetic On
  1. Have you enabled this option?
  2. Using datastores is kinda buggy if your running the game in the studio.