Help on pet equip

I made a pet equip script that seems to work fine but the problem is that it lets me equip the same exact pet twice and would like to know how to prevent that. When i click a gui in an inventory it brings up this ‘petinfo’ and when you press equip it equips but if you press equip multiple times on that same pet it will equip

I tried making an equip value in it but i clone the original pet which has it set to false so that doesnt work

Code:

script.Parent.MouseButton1Down:Connect(function()
	local petName = script.Parent.Parent.petName.Text
	
	if game.ReplicatedStorage.Assets.Pets[petName] then
		local petModel = game.ReplicatedStorage.Assets.Pets[petName]:Clone()
		local positions = {
			["1"] = 1,1,0,
			["2"] = 2,1,0
			}
		
		local amountEquipped = #game.Players.LocalPlayer.EquippedPets:GetChildren()
		if amountEquipped < 5 and petModel.Equipped.Value == false then
			petModel.Equipped.Value = true
			petModel.Parent = game.Players.LocalPlayer.Character
			petModel:SetPrimaryPartCFrame(game.Players.LocalPlayer.Character.HumanoidRootPart.CFrame)
			
			local attachChar = Instance.new("Attachment")
			attachChar.Visible = false
			attachChar.Parent = game.Players.LocalPlayer.Character.HumanoidRootPart
			attachChar.Position = Vector3.new(positions[amountEquipped + 1]) + petModel.PrimaryPart.Size
			
			local attachPet = Instance.new("Attachment")
			attachPet.Visible = false
			attachPet.Parent = petModel.PrimaryPart
			
			local alignPos = Instance.new("AlignPosition")
			alignPos.MaxForce = 25000
			alignPos.Attachment0 = attachPet
			alignPos.Attachment1 = attachChar
			alignPos.Responsiveness = 10
			alignPos.Parent = petModel
			
			local alignOr = Instance.new("AlignOrientation", petModel)
			alignOr.MaxTorque = 25000
			alignOr.Attachment0 = attachPet
			alignOr.Attachment1 = attachChar
			alignOr.Responsiveness = 10
			
			local newPet = Instance.new("StringValue", game.Players.LocalPlayer.EquippedPets)
			newPet.Name = petName
		end
	else
		print("No text or no pet found")
	end
end)
3 Likes

So it seems that you are not actually checking if the pet you are trying to equip is already equipped in a way that would ensure that it registered if the pet was already equipped. I would make sure each pet type either has a different name, and check that there isn’t already something in the player named that using FindFirstChild or if that doesn’t work for some reason, give each pet type an ID and loop through all the equipped pets to make sure that there isn’t one with a matching ID before equipping it.

i see,
so if i made the name of the value the pets name and the value a special id would that work

What you need is a way for you to determine which pets are already in the player. If all your pets have different names, and nothing else will end up as a child of the pet’s parent that could have a name match, then simply checking children for a matching name should be sufficient. If there are conflicts with naming for whatever reason, you could insert an ID value into each pet, and give that a unique identifier (name, number, etc.) that you know only that type of pet will have and check that for all pets before equipping a new pet to make sure there is no duplicate.

1 Like