Function won't run

I have no idea why, but print 2 isn’t running. I have never experienced something like this, so any help would be appreciated.

game.ReplicatedStorage:WaitForChild("PetRE"):WaitForChild("EquipPet").OnServerEvent:Connect(function(player,petName)
	
	local pet = game.ReplicatedStorage.Pets:WaitForChild(petName)
	local character = player.Character

	local petPositions = {
		{Position = Vector3.new(0,-1.5,7), Orientation = Vector3.new(0,-90,0)},
		{Position = Vector3.new(-7.5,-1.5,5), Orientation = Vector3.new(0,-120,0)},
		{Position = Vector3.new(7.5,-1.5,5), Orientation = Vector3.new(0,-60,0)}
	}

	local function GetPetOrder(character)
		local petFolder = character.Pets
		if #petFolder:GetChildren() == 0 then
			return 1
		end

		local activeSlots = {}
		for _, pet in ipairs(petFolder:GetChildren()) do
			local order = pet.Order.Value
			activeSlots[order] = order
		end

		for avalaibleSlot = 1, 5, 1 do
			if not activeSlots[avalaibleSlot] then
				return avalaibleSlot
			end
		end
	end
	print("1: defined getpetorder")
	
	local function PetFollow(player, pet)
		print("2: in petfollow function")
    end
5 Likes

From what you’ve provided, you aren’t actually calling the PetFollow function?

3 Likes

Oh yeah my bad. I was calling the function inside the function. But another problem arise. There is an in pairs loop inside the function. For some reason it can’t detect the textlabel named “NewSelect”. Maybe because of server/client side?

for i, v in pairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
	print("5: in for loop")
	if v.Name == "NewSelect" then
    print("6: found newselect")
    end
end
1 Like

Variables would help you, man. That’s a lot of extra space taken up in the first line for no reason.

If you’re calling that initially before actually creating “NewSelect”, that could be why. That if-statement will only run when once when it executes and if NewSelect isn’t there, then it skips past it.

1 Like

Regarding your first bit it’s easier for me to code like this.

I know very well what you are saying but NewSelect is created way before the for loop runs.

Well, even in the code you provided above, it still doesn’t appear you’re calling the PetFollow function. Are you sure you are?

Yep I am pretty sure. Let me give you a bigger view of the code. Print 5 runs but print 6 doesn’t.

Here is an image of the output:
image

game.ReplicatedStorage:WaitForChild("PetRE"):WaitForChild("EquipPet").OnServerEvent:Connect(function(player,petName)
	
	local pet = game.ReplicatedStorage.Pets:WaitForChild(petName)
	local character = player.Character

	local petPositions = {
		{Position = Vector3.new(0,-1.5,7), Orientation = Vector3.new(0,-90,0)},
		{Position = Vector3.new(-7.5,-1.5,5), Orientation = Vector3.new(0,-120,0)},
		{Position = Vector3.new(7.5,-1.5,5), Orientation = Vector3.new(0,-60,0)}
	}

	local function GetPetOrder(character)
		local petFolder = character.Pets
		if #petFolder:GetChildren() == 0 then
			return 1
		end

		local activeSlots = {}
		for _, pet in ipairs(petFolder:GetChildren()) do
			local order = pet.Order.Value
			activeSlots[order] = order
		end

		for avalaibleSlot = 1, 5, 1 do
			if not activeSlots[avalaibleSlot] then
				return avalaibleSlot
			end
		end
	end
	print("2: defiend getpetorder")
	
	local function PetFollow(player, pet)
		print("3: in petfollow function")
		if not character then return end
		print("4: no character")

		local petsFolder = character:FindFirstChild("Pets")
		if not petsFolder then
			petsFolder = Instance.new("Folder", character)
			petsFolder.Name = "Pets"
		end
		print("4: defined petsfolder")

		for i, v in pairs(player.PlayerGui.Inventory.Pets.ScrollingFrame:GetDescendants()) do
			print("5: in for loop")
			if v.Name == "NewSelect" then
				print("6: found newselect")
				if v.Text == "1" then
					print("7: text is 1")
					local petUUID = v.Parent.UUID.Value
					local petModel: Model = game.ReplicatedStorage.Pets:WaitForChild(petName):Clone()
					print("8: cloned pet")
					petModel.UUID.Value = petUUID
					petModel:PivotTo(character.HumanoidRootPart.CFrame)

					local charAttachment = Instance.new("Attachment", character.HumanoidRootPart)
					charAttachment.Visible = false

					local petAttachment = Instance.new("Attachment", petModel.PrimaryPart)
					petAttachment.Visible = false

					local alignPosition = Instance.new("AlignPosition", petModel)
					alignPosition.MaxForce = 25_000
					alignPosition.Attachment0 = petAttachment
					alignPosition.Attachment1 = charAttachment
					alignPosition.Responsiveness = 25

					local alignOrientation = Instance.new("AlignOrientation", petModel)
					alignOrientation.MaxTorque = 25_000
					alignOrientation.Attachment0 = petAttachment
					alignOrientation.Attachment1 = charAttachment
					alignOrientation.Responsiveness = 25

					local order = Instance.new("IntValue", petModel)
					order.Name = "Order"
					order.Value = GetPetOrder(character)

					local petPosition = petPositions[order.Value]
					charAttachment.Position = petPosition.Position
					charAttachment.Orientation = petPosition.Orientation

					petModel.Name = petName
					petModel.Parent = petsFolder
					print("9: put pet in character")
					petModel.PrimaryPart:SetNetworkOwner(player)
				end
			end
		end
	end
	
	PetFollow(player, pet)
end)

Okay so what you’ve provided me is different than what you initially provided in terms of where you said it printed to – where does it print to now?

I suggest printing v.Name before the if statement to observe its value and gain better insight.

1 Like

Here is what the output said:
image

Here is how it looks in the Explorer. I guess the reason the image button doesn’t get detected is because I’m moving it in a local script. I don’t know if moving the imagebutton in a serverscript would cause issues between the clients.
image

I suggest maintaining all UI-related actions on the client-side and reserving server invocation for specific scenarios, such as executing actions or obtaining data not available on the client.

1 Like

Yeah I got it working now. Thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.