-- Runs when someone touches our pet

function touch(part)

	-- IF player AND not dead AND has no master
	if part.Parent:FindFirstChild('Humanoid') and part.Parent:FindFirstChild('Humanoid').health > 0 and not master then

		-- Assign the master
		master = part.Parent.Head

		--Create a new spring
		spring = Instance.new("SpringConstraint", pet)
		spring.Attachment0 = pet.SpringAttach
		spring.Attachment1 = master.HatAttachment
		spring.FreeLength = maxDistance
		spring.Stiffness = 1000
		spring.Damping = 2

		-- Makes pet turn with player
		orient = Instance.new("AlignOrientation", pet)
		orient.Attachment0 = pet.OrientAttach
		orient.Attachment1 = master.HatAttachment
		orient.RigidityEnabled = true


		-- Reset pet apon death		
		function death()

			spring:Destroy()
			orient:Destroy()
			pet.CFrame = startpos
			master = nil

		end

		master.Parent.Humanoid.Died:Connect(death)



	end

end

pet.Touched:Connect(touch)

