Hello i am trying to add something to the table and save them but it is not working can you help me?
** Code:**
local DTS = game:GetService(“DataStoreService”)
local PetDatas = DTS:GetDataStore(“PetValuestestssss”)
local PetsModule = require(game.ReplicatedStorage.PetModule)
game.Players.PlayerAdded:Connect(function(plr)
local NewFolder = Instance.new(“Folder”,plr)
NewFolder.Name = “Pets”
local PetDatasSaveds,ms = pcall(function()
return PetDatas:GetAsync(plr.UserId)
end)
if PetDatasSaveds ~= nil then
for i,v in pairs(ms) do
if PetsModule.PetSystemPets[i] then
local NewPetFolder = Instance.new("Folder",NewFolder)
NewPetFolder.Name = v["Name"]
local NewRaritie = Instance.new("StringValue",NewPetFolder)
NewRaritie.Name = "Raritie"
local NewPetId = Instance.new("StringValue",NewPetFolder)
NewPetId.Name = "PetID"
local NewEquippedValue = Instance.new("BoolValue",NewPetFolder)
NewEquippedValue.Name = "Equipped"
NewEquippedValue.Value = v["Equipped"]
NewPetId.Value = v["PetID"]
NewRaritie.Value = v["Raritie"]
else
PetDatas:RemoveAsync(plr.UserId,v)
print("Your Pet Value Not Working.")
end
end
else
print("Pets Not Loaded.")
end
game.ReplicatedStorage.LoadClient:FireClient(plr)
end)
game.Players.PlayerRemoving:Connect(function(plr)
local UnlockedPets = plr:FindFirstChild(“Pets”)
local PetTableData = {}
for i,v in pairs(UnlockedPets:GetChildren()) do
if PetsModule.PetSystemPets[v.Name] then
for index,value in pairs(UnlockedPets:GetChildren()) do
PetTableData[value.Name] = {
PetName = value.Name,
Equipped = value.Equipped.Value,
PetID = value.PetID.Value,
Raritie = value.Raritie.Value
}
end
else
print("Your Pet Value Not Working.")
end
end
if PetTableData ~= nil then
local sc,ms = pcall(function()
PetDatas:SetAsync(plr.UserId, PetTableData)
end)
else
print("No Have Assets For Save Pet Table.")
end
I saw that your formatting was messed up and that your variable names aren’t very
descriptive so I tried to fix that for you to make it easier to read. I have also changed
the script a bit by removing a for loop that seemed unnecessary and adding a print
at the end for debugging. Hope this helps!
local DTS = game:GetService("DataStoreService")
local PetDatas = DTS:GetDataStore("PetValuestestssss")
local PetsModule = require(game.ReplicatedStorage.PetModule)
game.Players.PlayerAdded:Connect(function(player)
local NewFolder = Instance.new("Folder", player)
NewFolder.Name = "Pets"
local was_request_successful, requested_data = pcall(function()
return PetDatas:GetAsync(player.UserId)
end)
if was_request_successful == true then
for some_pet_index, some_pet_value in pairs(requested_data) do
if PetsModule.PetSystemPets[some_pet_index] then
local NewPetFolder = Instance.new("Folder",NewFolder)
NewPetFolder.Name = some_pet_value["Name"]
local NewRaritie = Instance.new("StringValue",NewPetFolder)
NewRaritie.Name = "Raritie"
local NewPetId = Instance.new("StringValue",NewPetFolder)
NewPetId.Name = "PetID"
local NewEquippedValue = Instance.new("BoolValue",NewPetFolder)
NewEquippedValue.Name = "Equipped"
NewEquippedValue.Value = some_pet_value["Equipped"]
NewPetId.Value = some_pet_value["PetID"]
NewRaritie.Value = some_pet_value["Raritie"]
else
PetDatas:RemoveAsync(player.UserId, some_pet_value)
print("Your Pet Value Not Working.")
end
end
else
print("Pets Not Loaded.")
end
game.ReplicatedStorage.LoadClient:FireClient(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
local UnlockedPets = player:FindFirstChild("Pets")
local PetTableData = {}
for _, unlocked_pet in pairs(UnlockedPets:GetChildren()) do
if PetsModule.PetSystemPets[unlocked_pet.Name] then
PetTableData[unlocked_pet.Name] = {
PetName = unlocked_pet.Name,
Equipped = unlocked_pet.Equipped.Value,
PetID = unlocked_pet.PetID.Value,
Raritie = unlocked_pet.Raritie.Value
}
else
print("Your Pet Value Not Working.")
end
end
if PetTableData ~= nil then
local was_request_successful, request_error = pcall(function()
PetDatas:SetAsync(player.UserId, PetTableData)
end)
print(was_request_successful, request_error)
else
print("No Have Assets For Save Pet Table.")
end
end)
Try printing out variables at different parts of the script to help with debugging.
I added some more try to run this and tell us if you get any output
local DTS = game:GetService("DataStoreService")
local PetDatas = DTS:GetDataStore("PetValuestestssss")
local PetsModule = require(game.ReplicatedStorage.PetModule)
game.Players.PlayerAdded:Connect(function(player)
local NewFolder = Instance.new("Folder", player)
NewFolder.Name = "Pets"
local was_request_successful, requested_data = pcall(function()
return PetDatas:GetAsync(player.UserId)
end)
if was_request_successful == true then
for some_pet_index, some_pet_value in pairs(requested_data) do
if PetsModule.PetSystemPets[some_pet_index] then
local NewPetFolder = Instance.new("Folder",NewFolder)
NewPetFolder.Name = some_pet_value["Name"]
local NewRaritie = Instance.new("StringValue",NewPetFolder)
NewRaritie.Name = "Raritie"
local NewPetId = Instance.new("StringValue",NewPetFolder)
NewPetId.Name = "PetID"
local NewEquippedValue = Instance.new("BoolValue",NewPetFolder)
NewEquippedValue.Name = "Equipped"
NewEquippedValue.Value = some_pet_value["Equipped"]
NewPetId.Value = some_pet_value["PetID"]
NewRaritie.Value = some_pet_value["Raritie"]
else
PetDatas:RemoveAsync(player.UserId, some_pet_value)
print("Your Pet Value Not Working.")
end
end
else
print("Pets Not Loaded.")
end
game.ReplicatedStorage.LoadClient:FireClient(player)
end)
game.Players.PlayerRemoving:Connect(function(player)
local UnlockedPets = player:FindFirstChild("Pets")
local PetTableData = {}
print("Removing Player:", player, "|", UnlockedPets)
for _, unlocked_pet in pairs(UnlockedPets:GetChildren()) do
print("Unlocked Pet:", "unlocked_pet")
if PetsModule.PetSystemPets[unlocked_pet.Name] then
PetTableData[unlocked_pet.Name] = {
PetName = unlocked_pet.Name,
Equipped = unlocked_pet.Equipped.Value,
PetID = unlocked_pet.PetID.Value,
Raritie = unlocked_pet.Raritie.Value
}
else
print("Your Pet Value Not Working.")
end
end
if PetTableData ~= nil then
local was_request_successful, request_error = pcall(function()
PetDatas:SetAsync(player.UserId, PetTableData)
end)
print(was_request_successful, request_error)
else
print("No Have Assets For Save Pet Table.")
end
end)
What is the error?
If no error, have you tried to checked the saved result with a datastore editor?
Have you tried to same a single data first to see if DS is working / enabled at all?