#data store not working, 0 errors

15 messages · Page 1 of 1 (latest)

charred spade
#
local dss = game:GetService("DataStoreService")
local playerds = dss:GetDataStore("playerds")

game.Players.PlayerAdded:Connect(function(player)
    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player
    
    local Level = Instance.new("IntValue")
    Level.Name = "Level"
    Level.Parent = leaderstats
    
    local playerId = player.UserId
    
    
    local data 
    local success, errormessage = pcall(function()
        data = playerds:GetAsync(playerId)
        end)
    
    
    if success then
        Level.Value = data
    end
end)


game.Players.PlayerRemoving:Connect(function(player)
    
    local playerId = player.UserId
    
    local success, errormessage = pcall(function()
        
        local data = player.leaderstats.Level.Value
        
        playerds:SetAsync(playerId, data)
        
    end)
    if success then 
        print("it worked :D")
    else
        print("oops")
        warn(errormessage)
    end
end)
#

ping me when u respond cuz im gon be afk

steady smelt
split notch
#

Go to Game Settings -> Security -> Enable Studio Access to API Services and make sure thats on

charred spade
charred spade
steady smelt
# charred spade yeahhh

Convert the function you connected to Players.PlayerRemoving to a named function, then add the following code:

Players.PlayerRemoving:Connect(function(...)
    -- ...
end)

-- To:

local function onPlayerRemoving(...)
   -- ...
end

Then:

-- Change to
Players.PlayerRemoving:Connect(onPlayerRemoving)

game:BindToClose(function()
    for _, player in Players:GetPlayers() do
        task.spawn(onPlayerRemoving, player)
    end
end)
#

The server shuts down when the last client disconnects, and that is always the case in a solo studio session

charred spade
#

is it just like spawn()???

steady smelt
#

Like any variable, you can choose any valid name

#

"_" is among those valid names. "_" is used to name variables whose existence is necessary to reach positional values, but will will never be used

#

Why name a variable something if it will never be called upon? "_" acts a blank space, so it's optimal over a random character