#Datastorage doesnt work

1 messages · Page 1 of 1 (latest)

verbal shoal
#
function PlayerLeaving(player)

    if sessionData [player.UserId] then

        local success = nil
        local errorMsg = nil
        local attemt = 1

        repeat
            success, errorMsg = pcall(function()
                database:SetAsync(player.UserId, sessionData[player.UserId])
            end)


            attemt += 1
            if not success then
                warn(errorMsg)
                task.wait(3)
            end
        until    success or attemt == 5

        if success then 
            print("data saved for", player.Name)

        else
            warn("unable to save for", player.Name)
        end

    end
end
Players.PlayerRemoving:Connect(PlayerLeaving)

remoteEvent.OnServerEvent:Connect(function(player)
    local Kills = player:WaitForChild("leaderstats"):WaitForChild("Kills")
    Kills.Value = Kills.Value + 1
end)```
#

both scripts are one

signal junco
#

your code is a little hard to read because its not indented correctly

#
Kills.Value = sessionData[player.UserId].Kills
    Kills.Changed:Connect(function()
    sessionData[player.UserId].Kills = Kills.Value
        
    Level.Value = sessionData[player.UserId].Level
    Level.Changed:Connect(function()
    sessionData[player.UserId].Level = Level.Value
            
    end)
    leaderstats.Parent = player

    end)
#

this part here is not correct

#

your not ending the function at to correct location

verbal shoal
#

Hey it doesnt work but I am not sure if i copied it right because if I only copy yours i dont see anything in the leaderbord

#
local remoteEvent = game.ReplicatedStorage:WaitForChild("Kill")
local Players = game:GetService("Players")

local DatastoreService = game:GetService("DataStoreService")
local dataStore = DatastoreService:GetDataStore("PlayerData")
local stats = {"Score", "Kills", "Level"}
local playerData = {}

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

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

    local Score = Instance.new("IntValue",leaderstats)
    Score.Name = "Score"
    Score.Value = 0

    local Kills = Instance.new("IntValue",leaderstats)
    Kills.Name = "Kills"
    Kills.Value = 0
    
    local Level = Instance.new("IntValue",leaderstats)
    Level.Name = "Level"
    Level.Value = 0
    
    
    game.Players.ChildAdded:Connect(function(player)
        task.wait(5) -- wait a bit to make sure the previous server saved this players data before we try to load it

        local success, data = nil, nil
        for attempt = 1, 5 do
            if attempt > 1 then task.wait(3) end
            success, data = pcall(dataStore.GetAsync, dataStore, player.UserId)
            if success == true then break end
        end

        if success == false then
            warn("Unable to get data for", player.UserId)
            player:Kick("Unable to load your data. Try again later")
            return
        end

        playerData[player] = data

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

        for index, stat in stats do
            local intValue = Instance.new("IntValue")
            intValue.Name = stat
            intValue.Value = data[stat]
            intValue.Parent = leaderstats
            intValue.Changed:Connect(function(value)
                data[stat] = value
            end)
        end
    end)
#
    game.Players.ChildRemoved:Connect(function(player)
        local data = playerData[player]
        if data == nil then return end
        playerData[player] = nil
        for attempt = 1, 5 do
            if attempt > 1 then task.wait(3) end
            local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, data)
            if success == true then
                print("Data saved for", player.UserId)
                return
            end
        end
        warn("Unable to save data for", player.UserId)
    end)

    game.ReplicatedStorage.Kill.OnServerEvent:Connect(function(player)
        player.leaderstats.Kills.Value += 1
    end)
end)```
#

but thanks

signal junco
#

you have a connection inside another connection

#

you now have a memory leak

verbal shoal
signal junco
#

but you should read the code i sent you

#

and try to understand it

verbal shoal
#

ServerScriptService.Leaderstats:31: attempt to index nil with 'Score'

#

I get as an error

#

at line 31

signal junco
#

the data is nil

#
local DatastoreService = game:GetService("DataStoreService")
local dataStore = DatastoreService:GetDataStore("PlayerData")
local stats = {"Score", "Kills", "Level"}
local playerData = {}

game.Players.ChildAdded:Connect(function(player)
    task.wait(5) -- wait a bit to make sure the previous server saved this players data before we try to load it

    local success, data = nil, nil
    for attempt = 1, 5 do
        if attempt > 1 then task.wait(3) end
        success, data = pcall(dataStore.GetAsync, dataStore, player.UserId)
        if success == true then break end
    end

    if success == false then
        warn("Unable to get data for", player.UserId)
        player:Kick("Unable to load your data. Try again later")
        return
    end

    data = data or {}
    playerData[player] = data

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

    for index, stat in stats do
        local intValue = Instance.new("IntValue")
        intValue.Name = stat
        intValue.Value = data[stat] or 0
        intValue.Parent = leaderstats
        intValue.Changed:Connect(function(value)
            data[stat] = value
        end)
    end
end)

game.Players.ChildRemoved:Connect(function(player)
    local data = playerData[player]
    if data == nil then return end
    playerData[player] = nil
    for attempt = 1, 5 do
        if attempt > 1 then task.wait(3) end
        local success, errorMessage = pcall(dataStore.SetAsync, dataStore, player.UserId, data)
        if success == true then
            print("Data saved for", player.UserId)
            return
        end
    end
    warn("Unable to save data for", player.UserId)
end)

game.ReplicatedStorage.Kill.OnServerEvent:Connect(function(player)
    player.leaderstats.Kills.Value += 1
end)
#

do you see what i changed

verbal shoal
#

ohh you change that when you dont have data it works also

signal junco
#

i did data = data or {} and intValue.Value = data[stat] or 0

verbal shoal
#

yes

signal junco
#

so if data is nil i make a new empty table

#

and if the value is nil i make it 0

verbal shoal
#

It works thanks

proper axle
signal junco
proper axle
#

Wait what