So I am making a pet data store its for my sim game and for some reason it doesn’t save it saves but it doesn’t load the data in.
I don’t know why but here’s the datastore script.
local dataStoreService = game:GetService("DataStoreService")
local petsDs = dataStoreService:GetDataStore("petsDs")
game.Players.PlayerAdded:Connect(function(player)
local equippedPet = Instance.new("StringValue", player)
equippedPet.Name = "EquippedPet"
local data = petsDs:GetAsync(player.UserId.."-pet")
if data then
for i, petName in pairs(data) do
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
local stringValue = Instance.new("StringValue")
stringValue.Name = petName
stringValue.Name = player.PetInventory
end
end
game.ReplicatedStorage.Events.PetEvents.SendPetData:FireClient(player,data)
print("Sent data to player")
else
print("Data not found.")
end
end)
local function savePets(player)
if player:FindFirstChild("PetInventory") then
local inventory = {}
for i, v in pairs(player.PetInventory:GetChildren()) do
table.insert(inventory, v.Name)
end
local success, errorMessage = pcall(function()
petsDs:GetAsync(player.UserId.."-pet",inventory)
end)
if success then
print("Player Pets Saved!")
else
warn("Pets not saved error: "..errorMessage)
end
end
if player:FindFirstChild("EquippedPet") then
if player.EquippedPet.Value ~= nil then
local success, errorMessage = pcall(function()
petsDs:SetAsync(player.UserId.."-equippedPet", player.EquippedPet.Value)
end)
end
end
end
game.Players.PlayerRemoving:Connect(function(player)
savePets(player)
end)
game:BindToClose(function(player)
savePets(player)
end)
now the event
game.ReplicatedStorage.Events.PetEvents.SendPetData.OnClientEvent:Connect(function(petNames)
for i, petName in pairs(petNames:GetChildren()) do
if game.ReplicatedStorage.Pets:FindFirstChild(petName) then
addToFrame(game.ReplicatedStorage.Pets:FindFirstChild(petName))
end
print("Event done!")
end
end)
invalid argument #1 to ‘pairs’ (table expected, got string)
for i, petName in pairs(data) do
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
local stringValue = Instance.new("StringValue")
stringValue.Name = petName
stringValue.Name = player.PetInventory
end
end
game.ReplicatedStorage.Events.PetEvents.SendPetData:FireClient(player,data)
print("Sent data to player")
else
print("Data not found.")
end
Well, yes. Considering you’re saving a string to the DataStore, it would be good to assume you’d get a string when you load it, and you can’t loop over a string.
local dataStoreService = game:GetService("DataStoreService")
local petsDs = dataStoreService:GetDataStore("petsDs")
game.Players.PlayerAdded:Connect(function(player)
local equippedPet = Instance.new("StringValue", player)
equippedPet.Name = "EquippedPet"
local data = petsDs:GetAsync(player.UserId.."-pet")
if data then
for i, petName in ipairs(data) do
if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(petName) then
local stringValue = Instance.new("StringValue")
stringValue.Name = petName
stringValue.Name = player.PetInventory
end
end
game.ReplicatedStorage.Events.PetEvents.SendPetData:FireClient(player,data)
print("Sent data to player")
else
print("Data not found.")
end
end)
local function savePets(player)
if player:FindFirstChild("PetInventory") then
local inventory = {}
for i, v in pairs(player.PetInventory:GetChildren()) do
table.insert(inventory, v.Name)
end
local success, errorMessage = pcall(function()
petsDs:GetAsync(player.UserId.."-pet",inventory)
end)
if success then
print("Player Pets Saved!")
else
warn("Pets not saved error: "..errorMessage)
end
end
if player:FindFirstChild("EquippedPet") then
if player.EquippedPet.Value ~= nil then
local success, errorMessage = pcall(function()
petsDs:SetAsync(player.UserId.."-pet", {player.EquippedPet.Value})
end)
end
end
end
game.Players.PlayerRemoving:Connect(function(player)
savePets(player)
end)
game:BindToClose(function(player)
savePets(player)
end)
game.ReplicatedStorage.Events.PetEvents.SendPetData.OnClientEvent:Connect(function(data)
local pet_name = game.ReplicatedStorage.Pets[data]
if pet_name then
addToFrame(pet_name)
end
end)