Re-pasting print("Loaded Data: ", data)

  • When I tried doing things on my own I have a weird error?

  • The data isn’t printing right, or is it? Whenever I go to the console I see the last values
    like: { Coins = 100, Health = 90, Hunger 40} but when I change it it doesn’t print the exact data I changed.

  • Any Help?

Script:

local dss: DataStoreService = game:GetService(‘DataStoreService’)
local ds: DataStore = dss:GetDataStore(‘ClientData’)

local serverstorage: ServerStorage = game:GetService(‘ServerStorage’)

game:GetService(‘Players’).PlayerAdded:Connect(function(player) – OnPlayerJoin

local data = ds.GetAsync(ds, player.UserId)

local succs = true

if not succs then player:Kick(“Server Couldn’t Load Data”) return end

for _, folder in serverstorage:FindFirstChild(‘ClientData’):GetChildren() do

  local dupfolder = folder:Clone()
  
  
  dupfolder.Parent = player		

end

print("Loaded Data: ", data)
end)

game:GetService(‘Players’).PlayerRemoving:Connect(function(player) – OnPlayerLeaving

local datatab = {Coins = 100, Aids = 90, Health = 90}

local data = ds:SetAsync(player.UserId, datatab)

print("Saved Data: ", data)

end)`

I will be only for a moment or so, but we be long…

  • gonna check this a few couple hours or so?

Might be wrong because it’s been forever last time I used normal DataStore.

This line should be the problem.

local data = ds:SetAsync(player.UserId, datatab) --> this won't return the updated data

you are only calling loaded data when you actually load in for the first time. Also, ds.GetAsync(ds, player.UserId) i dont thin kthis works. do ds:GetAsync(player.UserId)

I think you are not updating the data when the player is leaving. You are always saving the same values: {Coins = 100, Aids = 90, Health = 90} . That’s why you see the same values in the console, even when you change the data. I think this is how you should fix it:

local dss = game:GetService("DataStoreService")
local ds = dss:GetDataStore("ClientData")

local serverstorage = game:GetService("ServerStorage")

game:GetService("Players").PlayerAdded:Connect(function(player)
    local data = ds:GetAsync(player.UserId)

    local succs = true

    if not succs then
        player:Kick("Server Couldn't Load Data")
        return
    end

    for _, folder in ipairs(serverstorage:FindFirstChild("ClientData"):GetChildren()) do
        local dupfolder = folder:Clone()
        dupfolder.Parent = player
    end

    print("Loaded Data: ", data)
end)

game:GetService("Players").PlayerRemoving:Connect(function(player)
    -- Update the values based on the player's current data
    local datatab = {
        Coins = player.Coins.Value,
        Aids = player.Aids.Value,
        Health = player.Health.Value
    }

    local success, errorMessage = pcall(function()
        ds:SetAsync(player.UserId, datatab)
    end)

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

Thank you for helping the table was a simple test cause I haven’t implemented the values yet, thank you for the reminder.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.