Pet System Help

So I am trying to make a pet system in my roblox game I am currently making. I have made the hatching system and now I am trying to make a pet equip script. I am putting it in my leader stats script and everything works except when I go to change the value of the equipped pets to one of my pets I have in replicated storage it does not work. When the value changes it’s supposed to make that pet follow you but mine does nothing please help. Thanks in advance and here is the script:

local function equipPet(player,pet)

local character = player.Character

if pet == nil and character == nil then
	
	if character:FindFIrstChild(player.Name.."'s pet") then character(player.Name.."'s pet"):Destroy() end
	
	if character.HumanoidRootPart:FindFirstChild("attachmentCharacter") then
		character.HumanoidRootPart:FindFirstChild("attachmentCharacter"):Destroy()
	end
	
	pet.Name = player.Name.."'s pet"
	
	pet:SetPrimaryPartCFrame(character.HumanoidRootPart.CFrame)

	local modelSize = pet.PrimaryPart.size

	local attachmentCharacter = Instance.new("Attachment")
	attachmentCharacter.Visible = false
	attachmentCharacter.Name = "attachmentCharacter"
	attachmentCharacter.Parent = character.HumanoidRootPart
	attachmentCharacter.Position = Vector3.new(1,1,0) + modelSize
	
	local attachmentPet = Instance.new("Attachment")
	attachmentPet.Visible = false
	attachmentPet.Parent = pet.PrimaryPart

	local allignPosition = Instance.new("AlignPosition")
	allignPosition.MaxForce = 25000
	allignPosition.Attachment0 = attachmentPet
	allignPosition.Attachment1 = attachmentCharacter
	allignPosition.Responsiveness = 25
	allignPosition.Parent = pet
	
	local allignOrientation = Instance.new("AlignOrientation")
	allignOrientation.MaxTorque = 25000
	allignOrientation.Attachment0 = attachmentPet
	allignOrientation.Attachment1 = attachmentCharacter
	allignOrientation.Responsiveness = 25
	allignOrientation.Parent = pet

	pet.Parent = character
end

end

game.Players.PlayerAdded:Connect(function(player)

local leaderstats = Instance.new("Folder")
leaderstats.Name = "leaderstats"
leaderstats.Parent = player

local cash = Instance.new("IntValue")
cash.Name = "Cash"
cash.Value = 10000
cash.Parent = leaderstats

local Inventory = Instance.new("Folder")
Inventory.Name = "PetInventory"
Inventory.Parent = player

local equippedPet = Instance.new("StringValue")
equippedPet.Name = "EquippedPet"
equippedPet.Parent = player

player.CharacterAdded:Connect(function(char)
	if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
		equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
	end
end)

equippedPet.Changed:Connect(function()
	if equippedPet.Value == nil then
		if game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value) then
			equipPet(player,game.ReplicatedStorage:WaitForChild("Pets"):FindFirstChild(equippedPet.Value):Clone())
		end
	end
end)

end)

Do you mean if pet ~= nil and character ~= nil then?

1 Like

It still did not work but thank you for noticing that