Knife System in game

In my game, i have knives that deal damage when you click. I wan’t to add range versatility to these weapons so I can’t use .Touched. I would appreciate any insight into how I could create a system that would deal damage to the players when the knife is activated within a specific range. I have already tried using .magnitude, but it has been extremely iffy and i’m not sure where else to go. Thanks for your help.

I think you could raycast to the direction the player is using the knife, get the player that is within the raycast range, then do damage to that player

Personally I would recommend using raycasting as Dev_Peashie said.

-- Get players right in front of your player
HRP = game.Players.LocalPlayer.Character.HumanoidRootPart
local rayOrigin = HRP.Position
local rayDestination = (HRP.CFrame + HRP.CFrame.LookVector * range)
local rayCastResult = workspace:RayCast(rayOrigin, rayDestination)
1 Like

So should i raycast from knife position to the player look vector? Then would i just check if it was a player and then get the distance?

Do you mean that you want the hitbox of the knife to be bigger? If so, you can just make a hitbox part welded to the knife, and use .Touched on that.

1 Like

Thats my solution currently but i kinda dislike it so i wanted to see alternatives

This would be the best solution. Using raycasting or a variation of ZonePlus isn’t going to be worth it.

1 Like

yes, and then you could add an if statement asking if distance is a certain amount

Why not? Ray casting seems like a viable option to me

1 Like

In what way would you be using rays for a knife system? For collision detection?

you can send a ray to go to one direction and if it hits something on the way, it stops and returns the information needed (like what got hit and how long the raycast was)

1 Like

and with that info you could check if the thing that got hit was a player and if the length of the cast was how long you want it to be and damage that player

1 Like

Making the hitbox bigger or use raycasting at the end depends on your needs and how your system looks like.
If the ranges of knives are long enough so the “giant hitbox” looks silly, then use raycast is a perfect solution.