How to get a part to face me

alright this has probably been posted before and i would think you would use CFrame but i’m having trouble getting a part to face me.

local RS = game:GetService("ReplicatedStorage") --gets rstorage
local A1 = RS:WaitForChild("A1") --finds the remote event
local toolOne = nil -- define your tools if needed
local debounce = false

A1.OnServerEvent:Connect(function(plr, tool) --catches the signal made from the input
	print("fired event") --checks if the remote event fired
	print(plr, tool) -- checks who gave the signal and what weapon they have
	local plrChar = plr.Character --finds the signal giver's character
	local findTool = plrChar:FindFirstChild(tool.Name) --finds the tool name
	print(findTool) --double checks the tool name
	
	if findTool == nil then
		print("Player is not holding a weapon")
	 end
	
	local SelectedTool = tostring(findTool) --Transfers the value of findTool (an instance) to SelectedTool (a string)
	print(SelectedTool)


	if not findTool then -- runs if there is no tool 
		print("tool not found")
		return end

	-- TOOL ABILITIES GO HERE
	
	if SelectedTool == "Crusher" then --Runs if the player is holding Scythe
		print("Move Successful!")
		local attackDebounce = false
		local h = plrChar:WaitForChild("Humanoid")
		local animator = h:WaitForChild("Animator")
		local hrp = plrChar:WaitForChild("HumanoidRootPart")
		local rightLeg = plrChar:WaitForChild("Right Leg")
		local level = plr:WaitForChild("Level")
		local LookVector = hrp.CFrame.LookVector
		
		local animToPlay = script:FindFirstChild("Animation")
		local loadAnim = animator:LoadAnimation(animToPlay)
		
		loadAnim:Play()
		
		wait(.5)
		
		local fissure = RS:WaitForChild("Fissure"):Clone()
		fissure.Parent = workspace

		
		-- Build a "RaycastParams" object
		local raycastParams = RaycastParams.new()
		raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
		raycastParams.FilterDescendantsInstances = {plrChar}
		raycastParams.IgnoreWater = true
		
		local maxDistance = 10

		-- Cast the ray
		local origin = hrp
		local raycastResult = workspace:Raycast(origin.Position + Vector3.new(0,5,0), Vector3.new(30,-90,0).Unit * maxDistance, raycastParams)

		-- Interpret the result
		if raycastResult then
			print("Object/terrain hit:", raycastResult.Instance:GetFullName())
			print("Hit position:", raycastResult.Position)
			print("Surface normal at the point of intersection:", raycastResult.Normal)
			print("Material hit:", raycastResult.Material.Name)
			fissure.Position = raycastResult.Position + (LookVector * 5)
		else
			print("Nothing was hit!")
		end
		
		local Blade = tool:WaitForChild("Hammer").Blade

		local damage = 250 * (level.Value * 2.5)
		
		Blade.Touched:Connect(function(hit)
			if hit.Parent:FindFirstChild("Humanoid") and attackDebounce == false then
				hit.Parent.Humanoid:TakeDamage(damage)
				attackDebounce = true
				print(hit.Parent.Humanoid.Health)
			end
		end)
		wait(3.3)
		attackDebounce = false
		damage = 0
	end
	if findTool == toolOne then
		print("Player wants to do an action with this specific tool")
	end
end)

this is the code in the hammer (using script)

again, this has probably been posted before but i would like to know how.

1 Like

Instead of this,

fissure.Position = raycastResult.Position + (LookVector * 5)

You can do this:

fissure.CFrame = CFrame.new(raycastResult.Position + LookVector * 5) * CFrame.Angles(0, math.rad(hrp.CFrame.Rotation.Y), 0)

Add angle offsets using math.rad() in the CFrame.Angles area as needed.

Let me know if it worked!

so using the base script that you gave me, it now sets it at a set angle (which i believe is the set rotation of the model if viewed in studio), and not based on the rotation of the hrp, as it says the hrp has a rotation of 0

hrp.CFrame.Rotation.Y
CFrame rotations don’t contain a ‘Y’ component.

local origin = raycastResult.Position + LookVector * 5
fissure.CFrame = CFrame.lookAt(origin, origin + LookVector)

Use the CFrame.lookAt constructor. You may need to modify the above snippet slightly to fit your needs.

There is, it just returns 0.

Anyways, I realize that it does return 0, so I made this fix:

fissure.CFrame = CFrame.new(raycastResult.Position + LookVector * 5) * CFrame.Angles(0, math.rad(hrp.Orientation.Y), 0)

I should have clarified better, the Y component represents the CFrame’s Y-axis position but because the CFrame only holds rotational information, its ‘X’, ‘Y’ and ‘Z’ components are 0.

1 Like

i think one of the issues is that the union appears (in studio) slanted when at orientation 0,0,0, which i think causes the model to appear slanted

edit: ok now its always pointing sideways

edit 2: alright changed how it points (primary part orientation was changing how it was placed), but it offsets depending on which direction you are facing.

edit3: should’ve known it multiplies everything in the lookvector, and it only truly works when you’re facing positive z (unless it prints it differently)

alright i found out the problem, though many thanks for the help
when i raycasted it had a 30 degree offset on the x axis and that was the problem.