Database cant share the same data thought places

Well, I was creating a project that simulates old Roblox, like retrostudio, but when I got to the clothes part, I had an idea to pass the information to other places

I used a database nemed “player clothing” for save the clothing that the placer is using, but for some reason is not working!

I the start place is all ok, but when i get teleported to the another place the value of them is just vaped, and is set to nil

Yes, im with http options on, and tried re re-write the script, copy the original one and replaced but nothing worked for some reason

I have to use another database thing or function to work? Or redo the whole system?

1 Like

Are you using:

  • The correct key?
  • The correct store?
  • The same universe for both places?
  • A BindToClose subprogram?

I might need to see the script to help further.

1 Like
  1. check data persistence
  2. make sure ur script is working
  3. try debugging
  4. check is the data loading or not

can we see the code by any chance.

1 Like

First off if I was you I’d change datastore system, Roblox’s datastore is not efficient and often leads to data loss, I’d recommend using ProfileService, a table-based datastore system.

1 Like

ProfileService still uses Roblox DataStores, and you don’t need it to make a robust saving system if you know how to prevent data loss.

While ProfileService helps simplify saving, some people (like myself) prefer to make their own.

You just need to remember:

  • Data versioning
  • Budgeting
  • Saving tables
  • Multiple saving attempts
  • Checking for missing data
  • Using UpdateAsync to help compare data and further prevent loss
  • Other methods

But, yeah, it’s a really powerful module. All I’m saying is it’s not the only way to make robust data saving.

1 Like

Here is the script from the starter place:

``local RemoteEvent = game.ReplicatedStorage.SaveAvatar
local DataStoreService = game:GetService(“DataStoreService”)
local PlayerDataStore = DataStoreService:GetDataStore(“PlayerClothing”)

local function onRemoteEventFired(player, shirt, pants)
if not player or not player:IsA(“Player”) then
return
end
local data = {shirt,pants}

local success, errorMessage = pcall(function()
    PlayerDataStore:SetAsync(player.UserId, data)
end)

if not success then
    warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
else
    print("Shirt:"..data[1])
    print("Shirt:"..data[2])
    print("Data saved successfully for player " .. player.Name)
end

end

RemoteEvent.OnServerEvent:Connect(onRemoteEventFired)``

And the one on the place that set the data to nil:

``local Players = game:GetService(“Players”)
local DataStoreService = game:GetService(“DataStoreService”)
local PlayerDataStore = DataStoreService:GetDataStore(“PlayerClothing”)

local function onPlayerAdded(player)
print(“onPlayerAdded for”, player.Name)

local success, data = pcall(function()
    return PlayerDataStore:GetAsync(player.UserId)
end)

if success then
    print("Data retrieved successfully!")
    if data then
        print("Shirts id:", data.shirt)
        print("Pants:", data.pants)
        if data.shirt and data.pants then
            player.Character.Shirt.ShirtTemplate = data.shirt
            player.Character.Pants.PantsTemplate = data.pants
        end
    else
        print("No clothing data found for", player.Name)
    end
else
    warn("Failed to retrieve clothing data for player " .. player.Name)
end
player.CharacterAdded:Wait()

end
Players.PlayerAdded:Connect(onPlayerAdded)``

idk why is not working but ok

1 Like

You are never setting up this data - you do not have keys named “shirt” and “pants”. You simply made an array, meaning it would probably end up looking like this:

local data = {
    [1] = --[[shirt id]],
    [2] = --[[pants id]]
}

Instead, when you save your data, you need to make keys for it.

local data = {
    ["shirt"] = --[[shirt id]],
    ["pants"] = --[[pants id]]
}

local success, result = pcall(PlayerDataStore.UpdateAsync, PlayerDataStore, player.UserId, function(old) return data end)

Note that I used UpdateAsync because you can insert code there to help prevent data loss. Even if you don’t insert code, it still has certain safety features that SetAsync doesn’t. I would recommend implementing some of the other suggestions I made for robust data saving as well.

3 Likes

Thx! I will try that, for me database is a little confusing lol, i need to learn more of that

1 Like

So i tried your bug fix, but it didn’t work, still getting nil values

The nil place script:

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

local function onPlayerAdded(player)
    print("onPlayerAdded for", player.Name)

    local success, errormesssage, data = pcall(function()
        return PlayerDataStore:GetAsync(player.UserId)
    end)

    if success then
        print("Data retrieved successfully!")
        print("shirts: ",data)
        
        player.Character.Shirt.ShirtTemplate = data["shirts"]
        player.Character.Pants.PantsTemplate = data["pants"]
    else
        warn("Failed to retrieve clothing data for player " .. player.Name)
    end
    player.CharacterAdded:Wait()
end

Players.PlayerAdded:Connect(onPlayerAdded)

the starter place script:

local RemoteEvent = game.ReplicatedStorage.SaveAvatar
local DataStoreService = game:GetService("DataStoreService")
local PlayerDataStore = DataStoreService:GetDataStore("PlayerClothing")


local function onRemoteEventFired(player, shirtSave, pantsSave)
    if not player or not player:IsA("Player") then
        return
    end
    local data = {
        ["shirts"] = shirtSave,
        ["pants"] = pantsSave
    }

    local success, errorMessage = pcall(function()
        PlayerDataStore:SetAsync(player.UserId, data)
    end)


    if not success then
        
        warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
    else
        print("Shirt:"..data["shirts"])
        print("Pants:"..data["pants"])
        print("Data saved successfully for player " .. player.Name)
    end
end


RemoteEvent.OnServerEvent:Connect(onRemoteEventFired)

And the image of the error:

1 Like

Issue 1:

pcall gives two return values: the provided function’s success and its results. I’m guessing you got confused because sometimes people use it to only catch an error. GetAsync returns a DataStoreKeyInfo object, which you don’t really need to know about. That’s why it prints “Instance”, because the data is in your error message variable and the DataStoreKeyInfo is in your data variable.

local success, result = pcall(PlayerDataStore.GetAsync, PlayerDataStore, player.UserId)
if not success then
    warn(result)
    return nil
end

print(result)



Issue 2:

You aren’t waiting for the character to load before attempting to overwrite their shirt, resulting in your indexing nil error. You need to wait for them.

local char = player.Character or player.CharacterAdded:Wait()
local shirt = char:WaitForChild("Shirt")
local trousers = char:WaitForChild("Pants")
shirt.ShirtTemplate = data.shirts
trousers.PantsTemplate = data.pants
2 Likes

1 Like

Glad I could help! Let me know if you get anymore issues with it.

Make sure to mark this topic as solved so it can close!