(updated) How to make player face object when proximity prompt is activated

Hi,

I made a faucet that functions as expected, but I would like to make sure the script is written correctly?

local prompt = script.Parent
local anim = script.Parent.handwash
local SoundService = game:GetService("SoundService")
local pivotpart = script.Parent.Parent.Parent.Pivotpart

prompt.Triggered:Connect(function(player)
	local character = player.Character
	local hum = character:WaitForChild("Humanoid")
	local loadAnim = hum.Animator:LoadAnimation(anim)

	local faucetSound = Instance.new("Sound")
	faucetSound.SoundId = "rbxassetid://18306315701"
	faucetSound.Parent = script.Parent

	-- Save original WalkSpeed and JumpPower
	local originalWalkSpeed = hum.WalkSpeed
	local originalJumpPower = hum.JumpPower
	local originalJumpHeight = hum.JumpHeight

	-- Disable player movement
	hum.WalkSpeed = 0
	hum.JumpPower = 0
	hum.JumpHeight = 0

	-- Disable the prompt
	prompt.Enabled = false

	-- Move the player in front of the faucet and make them face it
	local HRP = character:WaitForChild("HumanoidRootPart")
	local offset = Vector3.new(0, 0, -2)  -- Adjust this offset based on how far in front of the faucet you want the player to stand
	local newPosition = pivotpart.Position + offset
	HRP.CFrame = CFrame.new(newPosition, pivotpart.Position)

	-- Play the animation
	loadAnim:Play()
	faucetSound:Play()

	-- Function to end the sequence early
	local function endSequence()
		loadAnim:Stop()
		faucetSound:Stop()
		prompt.Enabled = true
		hum.WalkSpeed = originalWalkSpeed
		hum.JumpPower = originalJumpPower
		hum.JumpHeight = originalJumpHeight

		-- Disconnect the jump connection
		if jumpingConnection then
			jumpingConnection:Disconnect()
			jumpingConnection = nil
		end
	end

	-- Listen for jump attempts
	local jumpingConnection = hum:GetPropertyChangedSignal("Jump"):Connect(function()
		if hum.Jump then
			endSequence()
		end
	end)

	-- Wait for the animation to finish
	wait(5)
	endSequence()
end)

I guess you could use LookVector?

1 Like
local HRP = character.HumanoidRootPart
HRP.CFrame = CFrame.lookAt(HRP.Position, Part.Position )

I think this can help

Okay guys ty for the help I seem to have figured it out. I used tp function. Thoughts on this code?

local prompt = script.Parent
local anim = script.Parent.handwash
local SoundService = game:GetService("SoundService")
local pivotpart = script.Parent.Parent.Parent.Pivotpart

prompt.Triggered:Connect(function(player)
	local character = player.Character
	local hum = character:WaitForChild("Humanoid")
	local loadAnim = hum.Animator:LoadAnimation(anim)

	local faucetSound = Instance.new("Sound")
	faucetSound.SoundId = "rbxassetid://18306315701"
	faucetSound.Parent = script.Parent

	-- Save original WalkSpeed and JumpPower
	local originalWalkSpeed = hum.WalkSpeed
	local originalJumpPower = hum.JumpPower
	local originalJumpHeight = hum.JumpHeight

	-- Disable player movement
	hum.WalkSpeed = 0
	hum.JumpPower = 0
	hum.JumpHeight = 0

	-- Disable the prompt
	prompt.Enabled = false

	-- Move the player in front of the faucet and make them face it
	local HRP = character:WaitForChild("HumanoidRootPart")
	local offset = Vector3.new(0, 0, -2)  -- Adjust this offset based on how far in front of the faucet you want the player to stand
	local newPosition = pivotpart.Position + offset
	HRP.CFrame = CFrame.new(newPosition, pivotpart.Position)

	-- Play the animation
	loadAnim:Play()
	faucetSound:Play()

	-- Function to end the sequence early
	local function endSequence()
		loadAnim:Stop()
		faucetSound:Stop()
		prompt.Enabled = true
		hum.WalkSpeed = originalWalkSpeed
		hum.JumpPower = originalJumpPower
		hum.JumpHeight = originalJumpHeight

		-- Disconnect the jump connection
		if jumpingConnection then
			jumpingConnection:Disconnect()
			jumpingConnection = nil
		end
	end

	-- Listen for jump attempts
	local jumpingConnection = hum:GetPropertyChangedSignal("Jump"):Connect(function()
		if hum.Jump then
			endSequence()
		end
	end)

	-- Wait for the animation to finish
	wait(5)
	endSequence()
end)


As long as it works it seems good to me.