Hello everyone,
I have a bug in my Datastore script and i really need help with it. The thing is that 2 Folder named: “Data” and “Pets” don’t get added inside a Player and so the player is not able to Get Pets or even Equip them. I don’t know how to Fix it and if you need any more informations i will give them just pls help me. Here is a Video of the Situation:
And here are the Scripts from the Pethandler and the DataStore if they help you:
Datastore
local Datastore = game:GetService("DataStoreService"):GetDataStore("PlayerData_4")
local RS = game:GetService("ReplicatedStorage")
function getLevel(totalXP)
local Increment = 0
local RequiredXP = 100
for i = 0, RS.Pets.Settings.MaxPetLevel.Value do
RequiredXP = 100 + (25*i)
if totalXP >= (100*i) + Increment then
if i ~= RS.Pets.Settings.MaxPetLevel.Value then
if totalXP < ((100*i) + Increment) + RequiredXP then
return i
end
else
return i
end
end
Increment = Increment+(i*25)
end
end
Players.PlayerRemoving:Connect(function(plr)
local Pets = plr.Pets
local Data = plr.Data
local PetData = {}
local PlayerData = {}
for _,PetObject in pairs(Pets:GetChildren()) do
PetData[#PetData + 1] = {
Name = PetObject.Name,
TotalXP = PetObject.TotalXP.Value,
Equipped = PetObject.Equipped.Value,
PetID = PetObject.PetID.Value,
Multiplier1 = PetObject.Multiplier1.Value,
Multiplier2 = PetObject.Multiplier2.Value,
Type = PetObject.Type.Value
}
end
for _,DataValue in pairs(Data:GetChildren()) do
PlayerData[#PlayerData + 1] = {
Name = DataValue.Name,
ClassName = DataValue.ClassName,
Value = DataValue.Value
}
end
Datastore:SetAsync(plr.UserId, {["PetData"] = PetData, ["PlayerData"] = PlayerData})
end)
Players.PlayerAdded:Connect(function(plr)
local Pets = Instance.new("Folder")
local Data = Instance.new("Folder")
local SavedData = Datastore:GetAsync(plr.UserId)
Pets.Name = "Pets"
Data.Name = "Data"
if SavedData ~= nil then
local PetData = SavedData.PetData
local PlayerData = SavedData.PlayerData
for i,v in pairs(PetData) do
local PetObject = RS.Pets.PetFolderTemplate:Clone()
local Settings = RS.Pets.Models:FindFirstChild(v.Name).Settings
local TypeNumber = RS.Pets.CraftingTiers:FindFirstChild(v.Type).Value
local Level = getLevel(v.TotalXP)
PetObject.Name = v.Name
PetObject.Equipped.Value = v.Equipped
PetObject.TotalXP.Value = v.TotalXP
PetObject.Multiplier1.Value = Settings.Multiplier1.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
PetObject.Multiplier2.Value = Settings.Multiplier2.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
PetObject.Multiplier4.Value = Settings.Multiplier4.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber) + (Settings.LevelIncrement.Value * Level)
PetObject.PetID.Value = v.PetID
PetObject.Type.Value = v.Type
PetObject.Parent = Pets
end
for i,v in pairs(script.Data:GetChildren()) do
local DataValue = v:Clone()
local DataTable = nil
DataValue.Parent = Data
for i,v in pairs(PlayerData) do
if v.Name == DataValue.Name then
DataTable = v
end
end
if DataTable ~= nil then
if DataValue.Name == "MaxStorage" or v.Name == "MaxEquipped" then
DataValue.Value = RS.Pets.Settings:FindFirstChild("Default".. DataValue.Name).Value
else
DataValue.Value = DataTable.Value
end
else
if DataValue.Name == "MaxStorage" or v.Name == "MaxEquipped" then
DataValue.Value = RS.Pets.Settings:FindFirstChild("Default".. DataValue.Name).Value
end
end
end
else
for i,v in pairs(script.Data:GetChildren()) do
local DataValue = v:Clone()
DataValue.Parent = Data
if DataValue.Name == "MaxStorage" or v.Name == "MaxEquipped" then
DataValue.Value = RS.Pets.Settings:FindFirstChild("Default".. DataValue.Name).Value
end
end
end
Pets.Parent = plr
Data.Parent = plr
end)
PetHandler
local RS = game.ReplicatedStorage
local MS = game:GetService("MarketplaceService")
function GetFolderFromPetID(Player, PetID)
for i,v in pairs(Player.Pets:GetChildren()) do
if v.PetID.Value == PetID then
return v
end
end
return nil
end
function GetPointOnCircle(CircleRadius, Degrees)
return Vector3.new(math.cos(math.rad(Degrees)) * CircleRadius, 1, math.sin(math.rad(Degrees))* CircleRadius)
end
function GetAllOfType(Player, PetName, Type)
local Pets = {}
for i,v in pairs(Player.Pets:GetChildren()) do
if v.Name == PetName then
if v:WaitForChild("Type").Value == Type then
Pets[#Pets + 1] = v.PetID.Value
end
end
end
if #Pets >= RS.Pets.Settings.PetsRequiredToCraft.Value then
return Pets
else
return nil
end
end
function GetNextType(TypeName)
local CurrentValue
for i,v in pairs(RS.Pets.CraftingTiers:GetChildren()) do
if v.Name == TypeName then
CurrentValue = v.Value
end
end
for i,v in pairs(RS.Pets.CraftingTiers:GetChildren()) do
if v.Value == CurrentValue + 1 then
return v.Name, CurrentValue + 1
end
end
end
function RandomID(Player)
local Rand = math.random(2,1000000)
for i,v in pairs(Player.Pets:GetChildren()) do
if v.PetID.Value == Rand then
return RandomID()
end
end
return Rand
end
function loadEquipped(Player)
local CurrentlyEquipped = {}
for _,Pet in pairs(Player:WaitForChild("Pets"):GetChildren()) do
if Pet.Equipped.Value == true then
CurrentlyEquipped[#CurrentlyEquipped + 1] = Pet.PetID.Value
else
local ExistentModel = nil
for _,Part in pairs(workspace.PlayerPets:FindFirstChild(Player.Name):GetChildren()) do
if Part:FindFirstChild("PetID") then
if Part.PetID.Value == Pet.PetID.Value then
ExistentModel = Part
end
end
end
if ExistentModel ~= nil then
ExistentModel:Destroy()
end
end
end
local Increment = 360/#CurrentlyEquipped
for i,v in pairs(CurrentlyEquipped) do
local ExistentModel = nil
local Folder = GetFolderFromPetID(Player, v)
for _,Part in pairs(workspace.PlayerPets:FindFirstChild(Player.Name):GetChildren()) do
if Part:FindFirstChild("PetID") then
if Part.PetID.Value == v then
ExistentModel = Part
end
end
end
if ExistentModel ~= nil then
ExistentModel.Pos.Value = GetPointOnCircle(RS.Pets.Settings.PetCircleRadius.Value, Increment * i)
else
local PetModel = RS.Pets.Models:FindFirstChild(Folder.Name):FindFirstChild(Folder.Type.Value):Clone()
PetModel.Pos.Value = GetPointOnCircle(RS.Pets.Settings.PetCircleRadius.Value, Increment * i)
PetModel.PetID.Value = v
PetModel.PetName.Value = Folder.Name
PetModel.Parent = workspace.PlayerPets:FindFirstChild(Player.Name)
PetModel.PrimaryPart:SetNetworkOwner(Player)
end
end
end
function ActionRequest(Player, Action, Parameters)
if Action == "Equip" then
local Folder = GetFolderFromPetID(Player, Parameters.PetID)
if Folder ~= nil then
if Folder.Equipped.Value == false then
local TotalEquipped = 0
for i,v in pairs(Player.Pets:GetChildren()) do
if v.Equipped.Value == true then
TotalEquipped = TotalEquipped + 1
end
end
if TotalEquipped < Player.Data.MaxEquip.Value then
Folder.Equipped.Value = true
loadEquipped(Player)
return "Success"
else
return "Error", "Too Many Pets Equipped"
end
else
return "Error", "Pet Already Equipped"
end
else
return "Error", "Invalid Pet"
end
elseif Action == "Unequip" then
local Folder = GetFolderFromPetID(Player, Parameters.PetID)
if Folder ~= nil then
if Folder.Equipped.Value == true then
Folder.Equipped.Value = false
loadEquipped(Player)
return "Success"
else
return "Error", "Pet Already Unequipped"
end
else
return "Error", "Invalid Pet"
end
elseif Action == "Delete" then
local Folder = GetFolderFromPetID(Player, Parameters.PetID)
if Folder ~= nil then
if Folder.Locked.Value == false then
Folder.Equipped.Value = false
loadEquipped(Player)
Folder:Destroy()
return "Success"
else
return "Error", "Pet Is Lock"
end
else
return "Error", "Invalid Pet"
end
elseif Action == "Lock" then
local Folder = GetFolderFromPetID(Player, Parameters.PetID)
if Folder ~= nil then
if Folder.Locked.Value == true then
Folder.Locked.Value = false
loadEquipped(Player)
return "BeenUnlocked"
else
Folder.Locked.Value = true
loadEquipped(Player)
return "BeenLocked"
end
end
elseif Action == "CheckLock" then
local Folder = GetFolderFromPetID(Player, Parameters.PetID)
if Folder ~= nil then
if Folder.Locked.Value == true then
loadEquipped(Player)
return "IsLocked"
elseif Folder.Locked.Value == false then
loadEquipped(Player)
return "IsUnlocked"
end
end
elseif Action == "UnlockAll" then
for i,v in pairs(Player.Pets:GetChildren()) do
if v.Locked.Value == true then
v.Locked.Value = false
end
end
elseif Action == "LockAllRarity" then
for i,v in pairs(Player.Pets:GetChildren()) do
if game.ReplicatedStorage.Pets.Models[v.Name].Settings.Rarity.Value == "Legendary" or game.ReplicatedStorage.Pets.Models[v.Name].Settings.Rarity.Value == "Mythical" or game.ReplicatedStorage.Pets.Models[v.Name].Settings.Rarity.Value == "Secret" then
if v.Locked.Value == false then
v.Locked.Value = true
end
end
end
elseif Action == "DeleteUnlocked" then
for i,v in pairs(Player.Pets:GetChildren()) do
if v.Locked.Value == false then
if v.Equipped.Value == false then
v:Destroy()
else
print("Did not destroy")
end
end
end
elseif Action == "Craft" then
local MainFolder = GetFolderFromPetID(Player, Parameters.PetID)
local MainType = MainFolder.Type.Value
local MainName = MainFolder.Name
if MainFolder ~= nil then
local Pets = GetAllOfType(Player, MainFolder.Name, MainFolder.Type.Value)
if Pets ~= nil then
for i = 1,RS.Pets.Settings.PetsRequiredToCraft.Value do
local Folder = GetFolderFromPetID(Player, Pets[i])
Folder.Equipped.Value = false
loadEquipped(Player)
Folder:Destroy()
end
local Clone = RS.Pets.PetFolderTemplate:Clone()
local Type, TypeNumber = GetNextType(MainType)
local Settings = RS.Pets.Models:FindFirstChild(MainName).Settings
Clone.PetID.Value = RandomID(Player)
Clone.Multiplier1.Value = Settings.Multiplier1.Value * (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber)
Clone.Multiplier2.Value = Settings.Multiplier2.Value* (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber)
Clone.Multiplier4.Value = Settings.Multiplier4.Value* (RS.Pets.Settings.CraftMultiplier.Value ^ TypeNumber)
Clone.Type.Value = Type
Clone.Parent = Player.Pets
Clone.Name = MainName
return Clone.PetID.Value
else
return "Error", "Not Enough Pets"
end
else
return "Error", "Invalid Pet"
end
elseif Action == "Mass Delete" then
for i,v in pairs(Parameters.Pets) do
local Folder = GetFolderFromPetID(Player, v)
if Folder ~= nil then
Folder.Equipped.Value = false
loadEquipped(Player)
Folder:Destroy()
end
end
elseif Action == "Unequip All" then
for i,v in pairs(Player.Pets:GetChildren()) do
if v.Equipped.Value == true then
v.Equipped.Value = false
loadEquipped(Player)
end
end
end
end
RS.RemoteEvents.PetActionRequest.OnServerInvoke = ActionRequest
RS.RemoteEvents.plrWalk.OnServerEvent:Connect(function(Player, State)
Player.Data:FindFirstChild("isWalking").Value = State
end)
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAppearanceLoaded:Connect(function()
if not workspace.PlayerPets:FindFirstChild(Player.Name) then
local Folder = Instance.new("Folder", workspace.PlayerPets)
Folder.Name = Player.Name
loadEquipped(Player)
end
end)
end)
game.Players.PlayerRemoving:Connect(function(Player)
local Folder = workspace.PlayerPets:FindFirstChild(Player.Name)
if Folder then
Folder:Destroy()
end
end)
-- Gamepasses
for i,v in pairs(RS.GamepassIDs:GetChildren()) do
MS.PromptGamePassPurchaseFinished:Connect(function(plr,ido,purchased)
if purchased and v.Value == ido then
if v.Name == "TripleOpen" then
plr.Data.TripleEggOwned.Value = true
elseif v.Name == "AutoOpen" then
plr.Data.AutoEggOwned.Value = true
elseif v.Name == "ExtraEquipped" then
plr.Data.MaxEquip.Value = RS.Pets.Settings.DefaultMaxEquipped.Value + 5
elseif v.Name == "ExtraStorage" then
plr.Data.MaxStorage.Value = RS.Pets.Settings.DefaultMaxStorage.Value + 2050
elseif v.Name == "XLStorage" then
plr.Data.MaxStorage.Value = RS.Pets.Settings.DefaultMaxStorage.Value + 150000
end
end
end)
game.Players.PlayerAdded:Connect(function(plr)
local Data = plr:WaitForChild("Data", math.huge)
if MS:UserOwnsGamePassAsync(plr.UserId, v.Value) then
if v.Name == "TripleOpen" then
plr.Data.TripleEggOwned.Value = true
elseif v.Name == "AutoOpen" then
plr.Data.AutoEggOwned.Value = true
elseif v.Name == "ExtraEquipped" then
plr.Data.MaxEquip.Value = RS.Pets.Settings.DefaultMaxEquipped.Value + 5
elseif v.Name == "ExtraStorage" then
plr.Data.MaxStorage.Value = RS.Pets.Settings.DefaultMaxStorage.Value + 2050
elseif v.Name == "XLStorage" then
plr.Data.MaxStorage.Value = RS.Pets.Settings.DefaultMaxStorage.Value + 150000
end
else
if v.Name == "ExtraEquipped" then
plr.Data.MaxEquip.Value = RS.Pets.Settings.DefaultMaxEquipped.Value
elseif v.Name == "ExtraStorage" then
plr.Data.MaxStorage.Value = RS.Pets.Settings.DefaultMaxStorage.Value
end
end
end)
end
-- Setup
for _,Folder in pairs(RS.Pets.Models:GetChildren()) do
for _,Model in pairs(Folder:GetChildren()) do
if Model.Name ~= "Settings" then
local PetID = script.PetSetup.PetID:Clone()
local PetName = script.PetSetup.PetName:Clone()
local Pos = script.PetSetup.Pos:Clone()
local CoinPos = script.PetSetup.CoinPos:Clone()
local Selected = script.PetSetup.Selected:Clone()
local Damaged = script.PetSetup.Damaged:Clone()
local BG = script.PetSetup.BodyGyro:Clone()
local BP = script.PetSetup.BodyPosition:Clone()
local Anim = script.PetSetup.AnimationController:Clone()
local FollowScript = script.PetSetup.Follow:Clone()
local LevelingScript = script.PetSetup.Leveling:Clone()
local Attachment = script.PetSetup.Attachment.Attachment:Clone()
local SelectionLine = script.PetSetup.SelectionLine:Clone()
PetID.Parent = Model
PetName.Parent = Model
Pos.Parent = Model
CoinPos.Parent = Model
Selected.Parent = Model
Damaged.Parent = Model
BG.Parent = Model.PrimaryPart
BP.Parent = Model.PrimaryPart
Anim.Parent = Model
FollowScript.Parent = Model
LevelingScript.Parent = Model
Attachment.Parent = Model.PrimaryPart
SelectionLine.Parent = Model
end
end
end
-- Global Pet Float
local maxFloat = .75
local floatInc = 0.035
local sw = false
local fl = 0
spawn(function()
while true do
wait()
if not sw then
fl = fl + floatInc
if fl >= maxFloat then
sw = true
end
else
fl = fl - floatInc
if fl <=-maxFloat then
sw = false
end
end
script.Parent.globalPetFloat.Value = fl
end
end)
Pls if you can tell me how to fix it☃️
