How do I create a part on the size and position and angle of a raycast weather it hit something or it did not

I really reallly do need to use raycast on this scenario so yeah your solution still does not helps because my original purpose was to expand the hitbox and even if i used this it brings the same problem of being hard to hit a player bc of the tiny hitbox, and also that method is client sided in some way to detect hits wich kinda makes my game insecure.

And also I wanna learn this to make lasers come out of guns that i will code wich if i were to do your solution it wouldnt help me fully learn what i asked to.

Oh yeah I forgot to mention that i want hitboxes to work on the server, Im fully aware that to get the mouse position you need a local script and firing a remote with that position to the server to make that.

oh, if that’s what you want to do then maybe this will help. Raycast Hitbox 4.01: For all your melee needs!

No you arent getting my point, That is a collision based hitbox because the hit is registered by your weapon hitting the enemy, I specifiied i didnt wanted a collision based engine and I wanted something like Minecraft for example, Where hits are registered by firing a raycast from your camera or head into your mouse direction, I have that system but now i wanna create a part on the raycast with its length and position and direction

oh okay, i get it now. do you mean something like this? How would I cast a ray to where my mouse hit, and then you’re creating a part with the length of the raycast with the same position as it? (like making the raycast visible?)

Yes exactly like that!, Thats where I’m stuck tho.

oh okay, well maybe this would help How do you visualize a raycast as a part?

Again as I said, I researched it and yes I found that but theres no explanation to what they are doing and leaves me confused, But I guess Il’l give it another read and see if I can understand it now.

oh okay, well. wait.

asdassdasdasd

can you try this script?

ListRayCastParams.FilterType = Enum.RaycastFilterType.Blacklist
	
	ListRayCastParams.FilterDescendantsInstances = {MyCharacter, FireAxeTool.FireAxeModel}
	
	-- Get Factors to create raycast
	local RayCastOrigin = MyCharHead.Position

	local RayCastDirection = (MousePosition - RayCastOrigin).Unit

	local RayCastResult = workspace:Raycast(RayCastOrigin, RayCastDirection*6, ListRayCastParams)
	
	-- Hitbox Creator --

if RayCastResult then
    local position = RayCastResult.Position
    local distance = (origin - result.Position).Magnitude
    local p = Instance.new("Part")
    p.Anchored = true
    p.CanCollide = false
    p.Size = Vector3.new(0.1, 0.1, distance)
    p.CFrame = CFrame.lookAt(RayCastOrigin, position)*CFrame.new(0, 0, -distance/2)
end

What about using Mouse.UnitRay and making the ray visible by calculating the midpoint between bullet’s starting position and Ray’s hit position or default set length? Ex:

local Players = game:GetService("Players")
local RS = game:GetService("RunService")

local Client = Players.LocalPlayer or Players:GetPropertyChangedSignal("LocalPlayer"):Wait()
local Mouse: Mouse = Client:GetMouse()

local BulletStartPos = script.Parent.StartPos

local function MakeRayVisible(pos1, pos2)
	local Distance = (pos1-pos2).Magnitude
	local MidPoint = (pos1+pos2)/2
	
	local NewPart = Instance.new("Part")
	NewPart.Anchored = true
	NewPart.CanCollide = false
	NewPart.Material = Enum.Material.Neon
	NewPart.Size = Vector3.new(.1, .1, Distance)
	NewPart.CFrame = CFrame.lookAt(MidPoint, pos2)
	NewPart.Parent = workspace
	
	return NewPart
end

RS.RenderStepped:Connect(function()
	local Origin, Direction = BulletStartPos.Position, Mouse.UnitRay.Direction*300
	local Result = workspace:Raycast(Origin, Direction)
	
	local Intersection = Result and Result.Position or Origin + Direction
	local VisibleRay = MakeRayVisible(Origin, Intersection)
end)

If I’m understanding this correctly it seems like you want to do all the hit detection through the part itself, and were just using the ray for obtaining the initial direction? If that’s the case there isn’t really any need for the ray.

You can try getting the direction using just the mouse position and player’s head, and then place a part of whatever desired hitbox size you want in front of the player.

local hitboxLength = 6
local hitboxWidth = 0.5
	
local hit = Mouse.Hit.p
local dir = (hit - Player.Character.Head.CFrame.p).unit
	
local part = Instance.new("Part")
part.Anchored = true
part.Size = Vector3.new(hitboxWidth, hitboxWidth, hitboxLength)
part.CFrame = CFrame.new(Player.Character.Head.CFrame.p + (hitboxLength / 2 * dir), hit)
part.Parent = workspace
-- handle whatever checks you need and clean up

Uhhh could you explain what it does on the Cframe and size part im kinda confused…

So i kinda need it to directly do what I said, Since yes I could do your method but I kinda want also to learn how to make a part on the raycast so i can make some effects for guns like lasers with parts and not with beams or particle emiters.

I wish I was such an advanced coder to understand this, Could you explain please what it does, sorry if this comes as an anoyance, I am legit struggling to understand most of this script.

the part gets positioned to the middle of the raycast, can you try it?

Oh I see but theres just one problem I need the result of the raycast but what if i dont hit anything.

it checks if it’s a true or nil in here

function a:BulletHole(Position,BulletSize,Normal) -- Position is RaycastResult.Position, Normal is RaycastResult.Normal
	local p = Instance.new("Part")
	p.CFrame = CFrame.new(Position, Position + Normal)
	p.Size = Vector3.new(BulletSize,BulletSize,0.1)
	p.Transparency = 1
	p.Anchored = true
	p.CanCollide = false
	p.Position = Position or assert(Position == nil,"No position passed in BulletHole method for Object " .. self.WeaponModel.Name)
	p.Parent = workspace.BulletHoles
	local d = Instance.new("Decal")
	d.Texture = "rbxassetid://4784881970"
	d.Face = "Front"
	d.Parent = p
	coroutine.wrap(function()
		wait(20) -- Disappear after 3 seconds
		p:Destroy()
	end)()
end

It basically casts a ray from where the bullet comes from to the Mouse’s pointing direction. If it finds something it will make the ray visible making a line which always starts from where the bullet comes to the Ray’s intersection with the Result. If there is no result, the line will go up to the range, which in this example is 300 studs.

I have been recently watching this thing called :ViewportPointToRay and the similar one, I think these might be the solution to my problem they use deprecated raycasts tho…