Hi all Devs, i have a problem doing my Pet Inventory system.
The problem is, when i click a pet to see it’s stats, the ouput give me an error saying:
Players.fraita23.PlayerGui.PetInventory.Handler:74: attempt to index nil with ‘Uncommon’
I have the pet hatch system too, so when i buy an egg the pet appears into the pet inventory gui, but like i said, the gui won’t show me the pet stats.
The scripts are:
Manager(LocalScript inside the ScreenGui)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Handler = require(script.Parent.Handler)
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local PetsConfig = require(ReplicatedStorage.Config.EggsConfig)
local PetModels = ReplicatedStorage.Pets
local player = Players.LocalPlayer
local screenGui = script.Parent
local frame = screenGui.Frame
local container = frame.PetsContainer
local info = frame.Info
local warning = frame.Warn
local petInfo = info.PetInfo
local equipBtn = petInfo.EquipBtn
local deleteButton = petInfo.DeleteButton
local exit = frame.ExitButton
--local template = script.Template
for _, pet in ipairs(player.petInventory:GetChildren()) do
local petTable = Handler.PetInstanceToTable(pet)
Handler.GeneratePet(petTable)
end
Handler.UpdatePetsInfo()
Handler.UpdatePetStored()
equipBtn.MouseButton1Click:Connect(function()
if not Handler.SelectedPet then return end
local result = Remotes.PetEquip:InvokeServer(Handler.SelectedPet.UUID)
if not result then return end
local petTemplateInstance = container:FindFirstChild(Handler.SelectedPet.UUID)
if result == "Equipped" then
petInfo.EquipBtn.Text = "Unequip"
petInfo.EquipBtn.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
petTemplateInstance.Holder.Equipped.Visible = true
Handler.UpdatePetsInfo()
elseif result == "Unequipped" then
petInfo.EquipBtn.Text = "Equip"
petInfo.EquipBtn.BackgroundColor3 = Color3.fromRGB(12, 190, 2)
petTemplateInstance.Holder.Equipped.Visible = false
Handler.UpdatePetsInfo()
elseif result == "Full" then
warning.Visible = true
task.delay(3, function()
warning.Visible = false
end)
end
end)
deleteButton.MouseButton1Click:Connect(function()
if not Handler.SelectedPet then return end
Remotes.PetDelete:FireServer(Handler.SelectedPet.UUID)
local petTemplateInstance = container:FindFirstChild(Handler.SelectedPet.UUID)
petTemplateInstance:Destroy()
petInfo.Visible = false
task.delay(.01, function()
Handler.UpdatePetStored()
Handler.UpdatePetsInfo()
end)
end)
exit.MouseButton1Click:Connect(function()
screenGui.Enabled = not screenGui.Enabled
end)
Remotes.PetHatched.OnClientEvent:Connect(function(player: Player, pet: table, petInstance: Instance)
local petTable = Handler.PetInstanceToTable(pet)
Handler.GeneratePet(petTable)
Handler.UpdatePetStored()
end)
Handler(ModuleScript inside the ScreenGui)
local Players = game:GetService("Players")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local PetsConfig = require(ReplicatedStorage.Config.EggsConfig)
local PetModels = ReplicatedStorage.Pets
local player = Players.LocalPlayer
local screenGui = script.Parent
local frame = screenGui.Frame
local container = frame.PetsContainer
local info = frame.Info
local stats = frame.Stats
local petInfo = info.PetInfo
local equippedStat = stats.Pets
local storageStat = stats.Storage
local exit = frame.ExitButton
local template = script.Template
local module = {}
module.SelectedPet = nil
function module.GetRarityColor(rarity: string)
local color
if rarity == "Common" then
color = Color3.fromRGB(173, 173, 173)
elseif rarity == "Uncommon" then
color = Color3.fromRGB(9, 176, 0)
elseif rarity == "Rare" then
color = Color3.fromRGB(19, 193, 164)
elseif rarity == "Secret" then
color = Color3.fromRGB(0, 0, 0)
elseif rarity == "Premium" then
color = Color3.fromRGB(170, 85, 255)
end
return color
end
function module.GenerateViewportFrame(viewportFrame: ViewportFrame, petModel: Model)
petModel:PivotTo(CFrame.new() * CFrame.Angles(0, math.rad(180), 0))
petModel.Parent = viewportFrame
local camera = Instance.new("Camera", viewportFrame)
viewportFrame.CurrentCamera = camera
camera.CFrame = CFrame.new(Vector3.new(-1,1,4), petModel.PrimaryPart.Position)
end
function module.UpdatePetStored()
local stored = #player.petInventory:GetChildren()
storageStat.Number.Text = "📦"..stored.."/"..player.petInfo.MaxStored.Value
end
function module.UpdatePetsInfo()
local equipped = 0
for _, pet in ipairs(player.petInventory:GetChildren()) do
if pet.Equipped.Value then
equipped += 1
end
end
equippedStat.Number.Text = "🐶"..equipped.."/"..player.petInfo.MaxEquipped.Value
end
function module.DisplayPetInfo(pet: table)
local petStat = PetsConfig[pet.ID][pet.Rarity]
local petInstance = player.petInventory:FindFirtsChild(pet.UUID)
petInfo.Namee.Text = pet.ID:gsub("_", " ")
petInfo.Rarity.Text = pet.Rarity
petInfo.Rarity.TextColor3 = module.GetRarityColor(pet.Rarity)
petInfo.StatsFrame.Dribbles.Text = "x"..petStat
petInfo.StatsFrame.Coins.Text = "x"..petStat
local previousModel = petInfo.ViewportFrame:FindFirstChild("Model")
if previousModel then
previousModel:Destroy()
end
local petModel = PetModels[pet.ID]:Clone()
module.GenerateViewportFrame(petInfo.ViewportFrame, petModel)
if petInstance.Equipped.Value then
petInfo.EquipBtn.Text = "Unequip"
petInfo.EquipBtn.BackgroundColor3 = Color3.fromRGB(255, 0, 4)
else
petInfo.EquipBtn.Text = "Equip"
petInfo.EquipBtn.BackgroundColor3 = Color3.fromRGB(12, 190, 2)
end
end
function module.GeneratePet(pet: table)
local clone = template:Clone()
clone.Visible = true
clone.Parent = container
clone.Name = pet.UUID
clone.Holder.PetName.Text = pet.ID:gsub("_", " ")
clone.Holder.Equipped.Visible = pet.Equipped
local petModel = PetModels[pet.ID]:Clone()
module.GenerateViewportFrame(clone.Holder.ViewportFrame, petModel)
clone.MouseButton1Click:Connect(function()
module.SelectedPet = pet
module.DisplayPetInfo(pet)
petInfo.Visible = true
end)
end
function module.PetInstanceToTable(pet: Instance)
local newpet = {}
newpet.UUID = pet.Name
newpet.ID = pet.ID.Value
newpet.Rarity = pet.Rarity.Value
newpet.Equipped = pet.Equipped.Value
return newpet
end
return module
Pets(ServerScriptService)
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")
local function AmountEquipped(player: Player)
local equipped = 0
for _, pet in ipairs(player.petInventory:GetChildren()) do
if pet.equipped.Value then
equipped += 1
end
end
return equipped
end
local function CanEquipPet(player: Player, uuid: string)
local maxEquipped = player.MaxEquipped.Value
local currentlyEquipped = AmountEquipped(player)
return currentlyEquipped < maxEquipped
end
local function PetEquip(player: Player, uuid: string)
local pet = player.petInventory:FindFirstChild(uuid)
if not pet then return end
if pet.Equipped.Value then
pet.Equipped.Value = false
return "Unequipped"
elseif CanEquipPet(player) then
pet.Equipped.Value = true
return "Equipped"
else
return "Full"
end
end
local function PetDelete(player: Player, uuid: string)
local pet = player.petInventory:FindFisrtChild(uuid)
if not pet then return end
pet:Destroy()
end
Remotes.PetEquip.OnServerInvoke = PetEquip
Remotes.PetDelete.OnServerEvent:Connect(PetDelete)
Thanks!