I’m Sorry For Repeat Making Similar Topic, So I Want To Make Clothing Data Store That If I Dies Or Leave, Saves Clothing That You Had.
Before, I Had The Error Called “Attempt To Index Nil With Number” And After Some Struggle, Now Outputs Doesn’t Show That Error, But Somehow, The Data Store Still Not Working. How Do I Fix This?. Outputs Not Shows Error.
local DataStoreService = game:GetService("DataStoreService")
local dataStore = DataStoreService:GetDataStore("ClothingSave")
local function SaveClothing(player)
local character = player.Character or player.CharacterAdded:Wait()
local TableToSave = {
character:WaitForChild("Shirt").ShirtTemplate,
character:WaitForChild("Pants").PantsTemplate
}
local success, err = pcall(function()
dataStore:SetAsync(player.UserId, TableToSave)
end)
if success then
print("Data has been saved!")
else
print("Data hasn't been saved!")
warn(err)
end
end
game.Players.PlayerAdded:Connect(function(player)
local character = player.Character or player.CharacterAdded:Wait()
local ShirtId = character:WaitForChild("Shirt").ShirtTemplate
local PantsId = character:WaitForChild("Pants").PantsTemplate
local data
local success, err = pcall(function()
data = dataStore:GetAsync(player.UserId)
end)
if success and data then
ShirtId = data[1]
PantsId = data[2]
else
print("The player has no data!")
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local success, err = pcall(function()
SaveClothing(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end)
game:BindToClose(function()
for _, player in pairs(game.Players:GetPlayers()) do
local success, err = pcall(function()
SaveClothing(player)
end)
if success then
print("Data has been saved")
else
print("Data has not been saved!")
end
end
end)