Need help with pet system!

Hello developers! I’ve been recently trying to create a pet feed system, and I cant get it to work!

Heres my code.

local pet = workspace:WaitForChild("Pet") -- pet object
local currentfood = script.CurrentFood.Value -- value 1
local foodtolevelup = script.FoodToLevelUp.Value -- value 2
local currentlevel = script.CurrentLevel.Value -- value 3
local UIS = game:GetService("UserInputService")
local owner = "Icy7812" -- my username.
local player = workspace:WaitForChild(owner)
local equipped = false -- equipped bool

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if not equipped then
			equipped = true
		end
	end
end)

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.R then
		if equipped then
			equipped = false
		end
	end
end)

while equipped do
	pet.Transparency = 0
	pet.Position = player.HumanoidRootPart.Position + Vector3.new(5,0,0)
	print("Pet is at "..pet.Position)
end

UIS.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.F then
		if equipped then
			if currentlevel == 1 then
				currentfood = currentfood + 1
				if currentfood >= foodtolevelup then
					currentlevel = 2 
					print("Pet leveled up!")
				end
			else
				print("Pet is at max level!")
			end
		end
	end
end)


if you have any suggestions please let me know!

hello? Why is no one solving this but your solving other problems?

What is the issue here lol

[chars]

trying to make a pet system where you press a key E and it equips the pet, and then the key R dis equips it. the pet also uses levels, you press F when the pet is equipped and feeds the pet and if you get a certain amount of food, it levels up.

Why are you constnatly setting the transparency and the position of the pet in this loop? Just create a function to where the pet is at a position using align position and orientation and set the transparency to zero like that

I just wanna know why you have your pet code on the client.

Can you give me all the code for the pet system? Because currently it seems kinda wierd I have a pet system myself too with everything.

local pet = workspace:WaitForChild("Pet") -- pet object
local currentfood = script.CurrentFood.Value -- value 1
local foodtolevelup = script.FoodToLevelUp.Value -- value 2
local currentlevel = script.CurrentLevel.Value -- value 3

local UIS = game:GetService("UserInputService")
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")

local owner = "Icy7812" -- my username.
local equipped = false -- equipped bool

UIS.InputBegan:Connect(function(input, processed)
	if processed then
		return
	end
	
	if input.KeyCode == Enum.KeyCode.E then
		if not equipped then
			equipped = true
		end
		
	elseif input.KeyCode == Enum.KeyCode.R then
		if equipped then
			equipped = false
		end
		
	elseif input.KeyCode == Enum.KeyCode.F then
		if equipped then
			if currentlevel == 1 then
				currentfood += 1
				if currentfood >= foodtolevelup then
					currentlevel = 2 
					print("Pet leveled up!")
				end
			else
				print("Pet is at max level!")
			end
		end
	end
end)

hrp:GetPropertyChangedSignal("Position"):Connect(function()
	pet.Position = hrp.Position + Vector3.new(5,0,0)
end)

There’s no need to create 3 separate callback functions each connected to an InputBegan event of the UserInputService, you can merge the 3 together, I’ve also used GetPropertyChangedSignal to listen for changes of the “Position” property of the HumanoidRootPart of the player’s character which is then used to update the current position of the pet instead of a while true do loop which causes the entire main thread to yield.

I’m very sorry for the late reply. Your script worked! Thanks for the help :slightly_smiling_face: