Raycast Hitbox 4.01: For all your melee needs!

You need to set up a Raycast filter to ignore the character.

Example
local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {char}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
local Hitbox = RaycastHitbox.new(Hitbox)
Hitbox.RaycastParams = raycastParams
2 Likes

I know this may sound dumb, but I can’t hit while moving anymore. Before I used to be able too, now I can’t. I believe that it is an animation bug since it animates the tool, I’m not sure tho. Can anyone help me with this?

Nevermind, I fixed it. It was cause my self hit detection :sweat_smile:

1 Like

Do you have any example to handle player ping issue that effecting the raycast?

What do you mean? Are you handling the Raycast on the server?

Nevermind, It’s fixed already, Thanks to HaxxerVaxxer’s post. I thought it was due to the player high ping.

I just doing some testing and for some odd reason, in studio, when I specifically “Run” the game, the raycast works and it’s visualizing. However, when I actually “Play” the game in studio there is no raycast and nothing visualized. Is anyone else having this problem or know how to fix this?

THANK YOU! i seriously can’t thank you enough, now i don’t have to deal with all of Touched’s problems

1 Like

I’ve made local script version, using 5 parts inside of the weapon which have to be welded to reference as points so its easily adjustable for all you have to do is have a part inside of a Tool that’s called Handle, and 5 parts called “Ray1”, Ray2"… “Ray5”. You get the point. After that you should have something like in the video. You also have to create a serverscript that will respond onserverevent of a remoteevent to actually damage the player. To add, removevent doesnt fire when ray hasn’t hit player, so the server can only really overload if 1000 players hit a player at the same time :smiley: With this module you will lag on 100 or less players just hitting air, promise ,since the server is generating all of the rays even they are not hitting anything important. :smiley:

local runService = game:GetService("RunService")

local Detecting = false

local tool = script.Parent

local Part = {tool:WaitForChild("Ray1"),tool:WaitForChild("Ray2"),tool:WaitForChild("Ray3"),tool:WaitForChild("Ray4"),tool:WaitForChild("Ray5")}

local WS = game:GetService("Workspace")

local Debris = game:GetService("Debris")

local player = game:GetService("Players").LocalPlayer

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {player.Character,tool}
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

local replicatedStorage = game:GetService("ReplicatedStorage")

local RayHitDetectionEvent = replicatedStorage.RayHitDetection.Hit --create a remote event in a folder called RayHitDetection in replicatedstorage

local RayPart = replicatedStorage.RayHitDetection.Ray --make ray part choose colour and texture rest dont really matter 

local RayCooldown = 1--type in seconds


local positionTable = {}
local SecondPositionTable = {}
local u = 0
local rayDelta = os.time()

local SwingDelta = os.time()

local SwingDelay = 3 --seconds



local function VisualiseNonHitRay(rayOrigin,rayDirection,rayDestination, i)
	
	local part = RayPart:Clone()
	part.Parent = Part[i]

	part.CFrame = CFrame.lookAt(rayOrigin,rayDestination)
	part.Size = Vector3.new(0,.1,(rayOrigin-rayDestination).Magnitude)
	Debris:AddItem(part, .5)
	
end

local function CreateRay(rayOrigin,rayDirection,rayDestination, i)
	
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	if raycastResult then
		
		local hitPart = raycastResult.Instance 
		
		if hitPart.Parent then
			
			if hitPart.Parent:FindFirstChild("Humanoid") then
				
				--print("Attacked Humanoid")
				
				if hitPart.Parent == player.Character then
				else
					
					if hitPart.Parent.Humanoid.Health>0 then
					
						RayHitDetectionEvent:FireServer(hitPart.Parent)
						
					end
				end
				
				
				
			end
			
		end
		
		
	else
		
		VisualiseNonHitRay(rayOrigin,rayDirection,rayDestination, i)
		
	end
	
end


function RayHitbox()
	rayDelta=os.time()
	while Detecting==true do 
		
		for i,v in pairs (Part) do
			
			positionTable[i] = v.Position
			
		end
		u=u+1
		
		
		runService.RenderStepped:Wait()
		
		for i,v in pairs (Part) do

			SecondPositionTable[i] = v.Position

		end
		
		for i,v in pairs(positionTable) do
			
			if (v-SecondPositionTable[i]).Magnitude > math.pi then
				
			else
				
				
				CreateRay(v,SecondPositionTable[i]-v,SecondPositionTable[i], i)
				
			end
			
		end
		
		if u == RayCooldown*60 then
			
			
			u=0
			Detecting=false
			coroutine.yield()
			
		end
		
		if os.time()-RayCooldown>rayDelta then
			
			rayDelta=os.time()
			Detecting=false
			coroutine.yield()
			
		end
		
	end
	coroutine.yield()
end



local function Start()
	
	if os.time()-SwingDelay > SwingDelta then

		SwingDelta = os.time()
		
		Detecting = true
		
		local Rays = coroutine.create(RayHitbox)
		
		coroutine.resume(Rays)
		
	end
	
	
	
	
end

tool.Equipped:Connect(function()

		tool.Activated:Connect(Start)

end)

–Edits, just improved a bit of code so you couldn’t press down and keep damaging people

Just use :GetTouchingParts() its more convenient

Turn off CanQuery in accessories so the raycasts from the module will ignore them

1 Like

I did that a couple months ago (right after posting it here) and it worked, thank you anyways!

1 Like

Every time I try using HitStart() it just spits out a error saying “attempt to call a nil value”. Nobody else seems to be having this issue, so I have no idea what I’m doing wrong.

it keeps hitting my humanoid. is there a way to fix this while having it still damage other players?

1 Like

Did you require() the module?

local RaycastHitbox = require(RaycastHitboxModule)

I literally copied the code from the GitHub tutorial with zero edits and keep getting an “attempt to index nil” error. I have the module in ReplicatedStorage and have a part with attachments just like the tutorial (taking things step by step to make sure it’s all working, which it isn’t) so I have no clue what I’m doing wrong. Might just try to write my own code from scratch at this rate…

1 Like

How would I enable debug mode ONLY on the client?
(I have made the hitboxes on the server)

1 Like

Can you get hitboxes on client side? My hitbox was created on server side, and i was wondering if you can interact with it on client.

Event.OnClientEvent:Connect(function(vizualize: BoolValue, HitboxObject)
	local hitbox = HitboxModule:GetHitbox(HitboxObject)
	if hitbox~=nil then
		hitbox.Visualizer=vizualize
	else
		warn("No hitbox found!")
	end
end)

(It says no hitbox found)

Did you mean so the client will see all the other players hitboxes including his own?
If not:
Make the hitboxes be visualized on client instead of using debug mode

--client side script
hitbox.Visualizer=true

Huh, can you please clarify?

chars