I’m trying to achieve this

But I have this

Heres the code that I have
// create the hitbox slightly ahead
CFrame.new((CharacterHRP.Position) + (CharacterHRP.CFrame.LookVector * 40))
I’m trying to achieve this

But I have this

Heres the code that I have
// create the hitbox slightly ahead
CFrame.new((CharacterHRP.Position) + (CharacterHRP.CFrame.LookVector * 40))
Hey! There’s lots of different ways to actually go about this, but, I’ll give my take on it. I made this up really quickly so it may be buggy and not 100% optimized, but it works.
There are settings I’ve wrote that you can edit to true/false if you’d like. My code is not exactly the best, but I hope it helps! ![]()
(This is a Server Script, place it inside of your part.)
-- // INITIALIZE
local Players=game:GetService('Players')
local Part=script.Parent
local DefaultCFrame=Part.CFrame
-- // SERVICE(s)
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local AdjustCooldown=false -- Debounce for SmoothTurning, don't mess with this.
-- // Settings
local TurnTime=.3 -- How long it takes for the orientation to readjust
local SmoothTurning = true -- If true, the part will turn smoothly, otherwise, it will snap, though this causes the part to be a bit delayed if set to true.
local LookAtPlayer = true -- If true, it will look directly at the player, otherwise, it will not follow the player's Y Axis (up and down)
-- // FUNCTIONS
local function GetClosestPlayer()
local RequiredDistance=15 -- Change this if you'd like
local Closest=nil
for _, Plr in pairs(Players:GetPlayers()) do
if Plr then
if Plr.Character then
local Distance=(Plr.Character.HumanoidRootPart.Position-Part.Position).Magnitude
if Distance <= RequiredDistance then
RequiredDistance=Distance
Closest=Plr.Character
end
end
end
end
return Closest
end
-- // MAKE PART LOOK AT LOCALPLAYER
RunService.Heartbeat:Connect(function()
local Player=GetClosestPlayer()
if Player then
if SmoothTurning then
if AdjustCooldown == false then
AdjustCooldown=true
task.delay(TurnTime, function() AdjustCooldown=false end)
if LookAtPlayer then
TweenService:Create(Part, TweenInfo.new(TurnTime), {CFrame=CFrame.lookAt(Part.Position, Player.HumanoidRootPart.Position)}):Play()
else
local cframe=CFrame.lookAt(Part.Position, Player.Player.HumanoidRootPart.Position.X,Part.Position.Y,Player.HumanoidRootPart.Position.Z)
TweenService:Create(Part, TweenInfo.new(TurnTime+.25), {CFrame=cframe}):Play()
end
end
else
if LookAtPlayer then
Part.CFrame.lookAt(Part.Position, Player.HumanoidRootPart.Position)
else
local cframe=CFrame.lookAt(Part.Position, Player.Player.HumanoidRootPart.Position.X,Part.Position.Y,Player.HumanoidRootPart.Position.Z)
Part.CFrame=cframe
end
end
else
TweenService:Create(Part, TweenInfo.new(.5), {CFrame=DefaultCFrame}):Play()
end
end)
It’s Simple. Use a WeldConstraint. When spawning the part, use a WeldConstraint to lock the part to the HumanoidRootPart’s rotation. I haven’t did hitboxes in a while, but it should work.
Something like this?
hitbox.CFrame = CharacterHRP.CFrame * CFrame.new(0,0,-40)