local Ds = game:GetService("DataStoreService")
local Save = "Dete - 6"
local Pet_Name = Ds:GetDataStore("Pet-Name"..Save)
local Pet_Level = Ds:GetDataStore("Pet-Level"..Save)
local Pet_Active = Ds:GetDataStore("Pet-Active"..Save)
game.Players.PlayerAdded:Connect(function(Player)
local Inventory = Instance.new("Folder", Player)
Inventory.Name = "Inventory"
local id = Player.userId
local Name = Pet_Name:GetAsync(id)
local lvl = Pet_Level:GetAsync(id)
local active = Pet_Active:GetAsync(id)
if Name and lvl and active then
for i, v in pairs(Name) do
print("Name: "..v)
local item = Instance.new("Folder", Inventory)
item.Name = v
for z, x in pairs(lvl) do
print("Level: "..x)
if not item:FindFirstChild("Level") then
local item_level = Instance.new("NumberValue", item)
item_level.Name = "Level"
item_level.Value = x
end
for l, b in pairs(active) do
print("Active: ")
print(b)
if not item:FindFirstChild("Equipped") then
local item_Active = Instance.new("BoolValue", item)
item_Active.Name = "Equipped"
item_Active.Value = b
end
end
end
end
end--]]
--[[ if Name and lvl and active then
for i, v in pairs(Name) do
for z, x in pairs(lvl) do
for l, b in pairs(active) do
if i == z == b then
print(v)
pcall(function()
local item = Instance.new("Folder", Inventory)
item.Name = v
local item_level = Instance.new("NumberValue", item)
item_level.Name = "Level"
item_level.Value = x
local item_Active = Instance.new("BoolValue", item)
item_Active.Name = "Equipped"
item_Active.Value = b
end)
end
end
end
end
end--]]
end)
game.Players.PlayerRemoving:Connect(function(Player)
local id = Player.userId
local PetName = {}
local PetLevel = {}
local PetActive = {}
for _,i in pairs(Player.Inventory:GetChildren()) do
print(i.Name)
table.insert(PetName, i.Name)
table.insert(PetLevel, i.Level.Value)
table.insert(PetActive, i.Equipped.Value)
end
Pet_Name:SetAsync(id, PetName)
Pet_Level:SetAsync(id, PetLevel)
Pet_Active:SetAsync(id, PetActive)
end)
Why is my pet script loading more then once?
1 answer
I can't say what your code is exactly doing after reading it, but I can assume with my experience that you were not supposed to put loops inside loops in this script.
Problem: Let's say you have saved names of pets, and let's say you have three pets. Just giving an example. Every pet has one level, which means you have three. Every pet has an active status, which is, again, three. You run loops inside loops which makes it 9 levels(Three for each) and 27 active status(9 for each) But that's only on loading phase.
If you would have four pets, with current script you have, it would cause you to have 16 levels and 64 active status load up.
Solution: As I said above, you were not supposed to put loops inside loops. I WOULD suggest you to seperate loops but that wouldn't work for you as you want to put level and active status in each pet. You don't name them unique(I think), so one loop and using all tables in it with the index we get with "in pairs" loop would work well for you.
What the hell am I talking about?? Please try to do it yourself. If you can't, then here's what I meant.
for i, v in pairs(Name) do
--Folder Thingy
print("Name: "..v)
local item = Instance.new("Folder", Inventory)
item.Name = v
--Level Thingy
local Level = lvl[i] --We can reach correct level by using index in your level table if you saved them in an order.
--You also used to check if there is a level folder. No need anymore as we do not run this code multiple times for same pet anymore.
local item_level = Instance.new("NumberValue", item)
item_level.Name = "Level"
item_level.Value = Level
--Active Thingy
local ActiveStatus = active[i] --Same logic as level table above
local item_Active = Instance.new("BoolValue", item)
item_Active.Name = "Equipped"
item_Active.Value = ActiveStatus
end
What we did?
- Got rid of wrong loop structure
- Reduced loop count
- Took advantage of index in "in pairs" loops.
- Got rid of unnecessary checks(Made them unnecessary)
Hope I could help!
This discussion is preserved as it was. New answers, comments, and votes are closed.

2 comments
When is it loading again?
Right when i go into test it load multiple times ;c