So I've been attempting to data save hats for the past couple of days. And now I'm getting this error that says table expected got nil on line 21, I am also getting the same error in player removing but instead of nil it says it got an object. I dont get how to fix this. Here it is
local dss = game:GetService("DataStoreService")
local myDataStore = dss:GetDataStore("Clothes")
game.Players.PlayerRemoving:Connect(function(plr)
local Wear = {}
for _,item in pairs(plr.Items) do
table.insert(Wear,item.Name)
end
pcall(function()
myDataStore:SetAsync(plr.UserId,Wear)
end)
end)
game.Players.PlayerAdded:Connect(function(plr)
local data
local Folder = Instance.new("Folder",plr)
Folder.Name = "Items"
pcall(function()
data = myDataStore:GetAsync(plr.UserId)
end)
for _,item in pairs(data) do
local Value = Instance.new("StringValue",Folder)
Value.Name = item
end
plr.CharacterAdded:Connect(function()
for i, accessory in pairs(Folder) do
game.ServerStorage.Everything[accessory.Name]:Clone().Parent = plr.Character
end
end)
end)

3 comments
how would i change the data variable if get async returns something that isn't nil
i made data into a table
Just add an if statement that checks if datastore:GetAsync(id) ~= nil then you make it data = datastore:GetAsync(id)