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?
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.
``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
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()
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.
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)
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