Hitbox that follows the player

Hi, I’ve been trying to make a sort of dash which has a hitbox that follows the player,. What would be the best method to make the hitbox stay in front of the player? Ive tried using part.Position = hrp.CFrame.Position but it didnt work.

game.ReplicatedStorage:WaitForChild(“dash”).OnServerEvent:Connect(function(plr)

local char = plr.Character
local hum = char:WaitForChild("Humanoid")
local hrp = char:WaitForChild("HumanoidRootPart")
local lv = Instance.new("LinearVelocity")
local attachment = hrp:WaitForChild("RootAttachment")
local animation = Instance.new("Animation")
animation.AnimationId ="http://www.roblox.com/asset/?id=id"
local animTrack = hum:LoadAnimation(animation)
animTrack:Play()
animTrack:GetMarkerReachedSignal("Dash"):Connect(function()
	local size = Vector3.new(7, 10, 8)

	local part = Instance.new("Part")
	local cf = hrp.CFrame * CFrame.new(0, 0, ((part.Size.Z/2) + 0.5)* -1)
	part.Position = hrp.CFrame.LookVector * 5
	part.Massless = true
	part.Transparency = 0
	part.Anchored = false
	part.CanCollide = false
	part.CanQuery = false
	part.CanTouch = false
	part.CFrame = cf
	part.Size = size
	part.Parent = workspace 
	part.Anchored = true
	local params = OverlapParams.new()
	params.FilterType = Enum.RaycastFilterType.Exclude
	params.CollisionGroup = "Default"
	params.MaxParts = 5
	params.FilterDescendantsInstances = {part}
	params:AddToFilter(char)
	local parts = workspace:GetPartBoundsInBox(cf, size, params)
	local hitcharacters = {}
	for i, part in pairs(parts) do
		if part.Parent:FindFirstChild("Humanoid") and not table.find(hitcharacters, part.Parent) then
			part.Parent:FindFirstChild("Humanoid").Health = part.Parent:FindFirstChild("Humanoid").Health - 35
			local ehrp = part.Parent:FindFirstChild("HumanoidRootPart")
			local uhrp = hrp
			local att = Instance.new("Attachment", ehrp)
			local lv = Instance.new("LinearVelocity", ehrp)
			lv.MaxForce = 9999999
			lv.Attachment0 = att
			lv.VectorVelocity = uhrp.CFrame.LookVector * 80
			game.Debris:AddItem(att, 0.1)
			table.insert(hitcharacters, part.Parent)
			
		end
		wait(0.1)

	end
	print(hitcharacters)
	part.CFrame = hrp.CFrame
	hitcharacters = {}
	--params:AddToFilter(hitcharacters)
	--print(params)

end)

end)

You would need to constantly update the position of the hitbox, presumably every frame, however this can be performance heavy and unnecessary.

Why don’t you just parent the hitbox to the player’s character and position it 3-5 studs in front of the player? Also you should use .CFrame so it takes rotation into account.

P.S this post should be under Scripting support

Thanks I changed the post to be under Scripting support, I parented the hitbox to the character, it still stays behind though?

alternatively you could weld the hitbox to the player

2 Likes

You could weld the hitbox to the player.

local function createHitbox(character)
    local torso = character:FindFirstChild("HumanoidRootPart") 
    if not torso then return end -- if no torso then dont run
    
    local hitbox = Instance.new("Part")
    hitbox.Size = Vector3.new(4, 5, 2) -- Adjust size as needed
    hitbox.Transparency = 1 -- Make it invisible (set to 0 to debug)
    hitbox.CanCollide = false
    hitbox.Anchored = false
    hitbox.Massless = true
    hitbox.Name = "Hitbox"
    hitbox.Parent = character
    
    local weld = Instance.new("WeldConstraint")
    weld.Part0 = torso
    weld.Part1 = hitbox
    weld.Parent = hitbox
end

-- Connect to CharacterAdded event
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(createHitbox)
end)

You could literally just weld the hitbox to the player after setting the initial position, I think someone posted it up top

this code makes my eyes hurt just weld it

just weld the hitbox to hrp bruh

bro honestly just weld it

weld the hitbox to the humanoid root part

local weld = Instance.new("Weld")
weld.C0 = CFrame.new(0,0,-1) -- -1 is to be in front of the character
-- you can adjust C0 to move the hitbox around with respect to the HumanoidRootPart
-- though if it were me I would just center it on the character.
weld.C1 = CFrame.identity
weld.Part0 = hitboxpart
weld.Part1 = Char.HumanoidRootPart
weld.Parent = hitboxpart
-- note you should only do this once each time the character spawns