Pet script not working

hi! I have this pet script that is supposed to spawn in the equipped pet once the player’s character loads in. it doesn’t return any errors and the script to spawn in the pet and move it around was working perfectly before I added the function to check attributes.
here’s the code:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		
		for _, pet in ipairs(player.Inventory.Pets:GetChildren()) do
			if pet:GetAttribute("ActivePet") and pet:GetAttribute("Owner") == player.Name then
				if player and character then
					local humRootPart = character.HumanoidRootPart
					local newPet = pet:Clone()
					newPet.Parent = character

					local bodyPos = Instance.new("BodyPosition", pet.cube)
					bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					local bodyGyro = Instance.new("BodyGyro", pet.cube)
					bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

					while wait() do
						bodyPos.Position = humRootPart.Position + Vector3.new(2.5,-1,2.5)
						bodyGyro.CFrame = humRootPart.CFrame
					end
				end
			end
		end
		
	end)
end)

here’s the explorer hierarchy for where pets are:
Screen Shot 2021-02-27 at 7.33.37 AM

and here’s the attributes of the pet:
Screen Shot 2021-02-27 at 7.38.39 AM
I’ve verified that attributes are updating as they should, so I think its just a problem with the script. idk how to fix this, can anyone help?

got it…U didn’t set the owner’s name in attributes…! put the name of the owner…in attribute…

when the player gets in the game.loop through every pets the player has and set the “Owner” name to the player’s name and set the equipped pet’s “ActivePet” to true…Now check …! If this works then ok…

nah there’s a script that updates the owner attribute, that part works just fine

ok when u run the game, go to the localplayer and see if the equipped pet’s “ActivePet” is true or not and check the “Owner” attribute also…maybe u can find a clue or two. Then inform me…ok?

Screen Shot 2021-02-27 at 8.05.41 AM
here’s what it looks like after running the script to equip it

ok wait i will test this too and see how i can solve this then i’ll inform u…

The script thats setting the owner attr might have ran after the for loop was finished

1 Like

Instead of checking if the pet attribute exists you should wait until it exists

1 Like

yes .I agree with that .I think the script ran after the charAdded event. So that’s why it isn’t detecting anymore…I guess

oh I should’ve clarified in the original post, I reset my character too to make sure the character was readded and it had no effect

bro I wrote this script.And It works fine.

game.Players.PlayerAdded:Connect(function(plr)
	local folder = Instance.new("Folder", plr)
	folder.Name = "Inventory"
	local pet = Instance.new("Part", folder)
	pet.Name = "MyPet"
	pet:SetAttribute("ActivePet", true)
	pet:SetAttribute("Owner", plr.Name)
	plr.CharacterAdded:Connect(function(char)
		for _, pet in ipairs(plr.Inventory:GetChildren()) do
			if pet:GetAttribute("ActivePet") and pet:GetAttribute("Owner") == plr.Name then
				if plr and char then
					local humRootPart = char.HumanoidRootPart
					local newPet = pet:Clone()
					newPet.Parent = char

					local bodyPos = Instance.new("BodyPosition", pet)
					bodyPos.MaxForce = Vector3.new(math.huge, math.huge, math.huge)

					local bodyGyro = Instance.new("BodyGyro", pet)
					bodyGyro.MaxTorque = Vector3.new(math.huge, math.huge, math.huge)

					while wait() do
						bodyPos.Position = humRootPart.Position + Vector3.new(2.5,-1,2.5)
						bodyGyro.CFrame = humRootPart.CFrame
					end
				end
			end
		end

	end)
end)```

but the pet doesn't move around ..U need another script to move the pet..maybe..

okay I added prints and I think I found the main issue. it looks like if pet:GetAttribute("ActivePet") is returning false for all pets no matter when I run the code, even if it’s been manually set to true. idk how to fix that though