Help with raycast

Hello!
I was working on a weapon hitbox using raycasting, which is attached to… well, two attachments.
Since I am new to raycasting and I am such a little baby :baby: , it doesn’t work (real shocker there…)

Heres my script: (not full, just raycast)

local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {tool.Parent}
params.IgnoreWater = true

ray = workspace:Raycast(attach0.WorldPosition, attach1.WorldPosition, params)

Any help is appreciated!

RayCast() takes a Direction as its second argument, not an end position. You can read more about raycasting here.

Assuming you want a ray to go from attach0 to attach1, you might want to do this instead:

local origin = attach0.WorldPosition
local direction = attach1.WorldPosition - attach0.WorldPosition

ray = workspace:Raycast(origin, direction, params)

Ok. I will try this. sorry for late reply

Im not sure if its my problem, but the weapon hitbox doesnt work.
I used your script with some changes for debugging, yet it didnt work.

Am I missing something?

Where are the attachments located in world space, and why are attachment positions being used in particular?

Im trying to make a weapon that does damage using raycasts cause .touched is unreliable.
The attachments are inside of a part of a weapon and scale from the bottom of the blade to the top

I am using attachments so I dont have to create a ton of parts

A .Touched event might be more suitable for a sword than rays. If you’re worried about the event being unreliable, you could add a debounce or lock variable to stop it from firing too many times in one swing.

Yeah, I tried a touched event already though.

Lets say it didnt go to plan…

It fired constantly even after I put a debounce.

If you wanted rays you could just use this module; Raycast Hitbox 4.01: For all your melee needs!

I already looked at that. It does not suit me very well, and I would much rather script it myself because thats how I learn. I dont learn by just slapping models into my tool

Its a module not a model and if you wanted I can help you with raycasts

I dont want someone to personally teach me, I find more fun in scripting by learning it myself and sometimes asking for help

Understandable, but here is what I did if you needed help; local origin = ((yourorigin)) local direction = (mouse.Hit.Position - origin).Unit * MaxRayDistance local rayParams = RaycastParams.new() rayParams.FilterDescendantsInstances = {workspace.Baseplate, player.Character} rayParams.FilterType = Enum.RaycastFilterType.Blacklist local result = workspace:Raycast(origin, direction, rayParams)

1 Like

Okay. Thanks for the help. I appreciate it

1 Like

I appreciate your enthusiasm to learn and try things out for yourself, but it’s much easier to use existing code than reinvent the wheel. Besides, nothing stops you from examining the module’s code and learning it that way, even if you don’t want to use it.

Aside from that, if the module didn’t suit you, it’s unlikely that using raycasting techniques for hitboxes will either; I’d still recommend using a .Touched event. Did you enable the debounce variable correctly when you tried it last time?

Touched events are still very unreliable. A magnitude check might be better for accuracy.

Yes. When I used the touched event, I made it able to disconnect so it wouldnt do damage when the sword wasnt swinging. That didnt change anything, though. When the sword already swung, it did damage after the animation was done

Exactly. My friend made a touched event weapon and it fell into shambles. Touched events are also heavily unreliable for falling objects that do damage. If you just stand still, the touched event wont fire.

You could use instance to make the hitbox part and cframe and parent it to the area you want it and then destroy it using :Destroy() make sure you use debounce

Yeah, but I feel like it would… feel unreal.
I also tried that on another script, it just didnt feel right to me.

The only option I have right now to fix the ray is fire the ray from the characters HumanoidRootPart. But that would mess up all the hitboxs’