GivePet Command not working properly

Hey Forumers, I’ve been making a simulator game, which has pets that you can hatch. Well, I made a command that makes it so i can give pets simply by typing the command in the chat. It worked and all when i was using the pet name to determine which pet to give, so i then tried to make it so that if the player types the command, but instead of the pet’s name it used the pet Id which is determined under a module script, but its not working for some reasons. this is my current givePet script:

--[ Library ]--
local _R = nil
local RunService = game:GetService("RunService")
if RunService:IsServer() then
	_R = require(game:GetService("ServerStorage"):WaitForChild("RobloxianFramework"))
else
	_R = require(game:GetService("ReplicatedStorage"):WaitForChild("RobloxianFramework"))
end
while not _R.Loaded do
	game:GetService("RunService").Heartbeat:Wait();
end;
local wait = _R.AccurateWait

--[ Script ]--
return {
	Name = "GivePet",
	MinimumRank = "Owner", -- Minimum rank of 2 for this command
	Usages = {"pet"},
	ParamFormat = {"Player", "Number", "Number"},
	Callback = function(executor, players, PetId, Power)
		local ReplicatedStorage = game:GetService("ReplicatedStorage")
		local PetsFolder = ReplicatedStorage:WaitForChild("Pets")

		local foundPetData = nil

		-- Iterate through each pet folder to find the correct PetId
		for _, petFolder in pairs(PetsFolder:GetChildren()) do
			print("Checking folder: " .. petFolder.Name)
			if petFolder:IsA("Folder") then
				local petModuleScript = petFolder:FindFirstChild("PetData")
				if petModuleScript then
					print("Found module script in folder: " .. petFolder.Name)
					local petData = require(petModuleScript)
					print("Pet Id from module script: " .. tostring(petData.PetId))
					print("Pet Id provided: " .. tostring(PetId))
					if petData.PetId == PetId then
						foundPetData = petData
						break
					end
				else
					warn("Module script not found in folder: " .. petFolder.Name)
				end
			end
		end

		if foundPetData then
			print("Creating pet with PetId: " .. tostring(foundPetData.PetId))
			for _, plr in pairs(players) do
				task.spawn(function()
					local success, errorMessage = pcall(function()
						_R.Pets.create(plr, foundPetData.PetId, "Normal", Power)
					end)
					if not success then
						warn("Error creating pet: " .. errorMessage)
					end
				end)
			end
			return true
		else
			warn("Pet module not found for PetId: " .. tostring(PetId))
			return false
		end
	end
}

it should be working, since all the prints are being printed properly, except the ones in the foundPetData block.

If you’re wondering, this is my module script in the pets that determines the petId:

local PetData = {
	PetName = "4 Eyes",
	PetId = "1",
	Rarity = "Secret",
	PetArea = "Area : 🏙️Spawn",
	Models = {},
	Huge = false,
	MinPower = 69,
	MaxPower = 6969,
	RecordPetExist = true,
	Mode = "Hover", --Walk/Hover
	HideCollection = {
		Normal = true,
		Amethyst = true,
		E = true,
		Rainbow = true
	},
}

return PetData

any help is highly generous :slight_smile:

It’s odd that it isn’t working. I assume the types are correct, given they both should be strings, however can you please just confirm this by adding another print statement,

print(typeof(petData.PetId), typeof(PetId))

oh gosh darn, i mightve messed something up?
image

(apologies for the late reply, what should i do?)

Thought they may have been the case…

Just put both in tostring() within the if statement, and it should work

i.e.

if tostring(petData.PetId) == tostring(PetId) then

or technically even just

if petData.PetId == tostring(PetId) then

I figured it all out by myself surprisingly actually, first i changed the PetId = “1”, to without the “” in the pets scripts, and then there were this warn which messed everything up, which was this:

Infinite yield possible on ‘ReplicatedStorage.Pets:WaitForChild(“1”)’

I think it did that warn since it expected the pet’s name to be “1”, so what i just did was i changed the part where it tried setting the foundPetData to the petId to this, so that it could actually get the pet’s name:

				if petData.PetId == PetId then
					foundPetData = {
						PetId = petData.PetId,
						PetName = petData.PetName
					}
					break
				end

and after this i just switched the foundPetData.PetId to foundPetData.PetName where it creates the pet and now it works great

tldr: its fixed, no need to read all this :+1: (thanks for the help tho)