I need help understanding how to use the Raycast Hitbox Module

I am a fairly new to scripting starting about 2.5 weeks ago, so I’m sorry if this post is annoying.

I’ve been trying to make a Sword combat system, so far, I’ve made four different types of simple HitReg systems by using Raycast, Magnitude, OnTouched, and Parts in Region3. Each of these HitReg Systems worked but didn’t reach the product I wanted to get. I wanted to have a HitReg System that actually was the hitbox of the blade, similar to Mordhau’s style of HitReg. So, I looked and eventually found Swordphin123’s Raycast Hitbox Module which I have spent quite a bit of time trying to learn the basics and syntax of.

So I’ve been testing with this script which is suppose to stop the firing of he rays once it hits something.

local Tool = script.Parent
local player = Tool.Parent.Parent
local character = player.Character or player.CharacterAdded:Wait()
local RaycastHitbox = require(Tool.RaycastHitboxV2)


local NewHitbox = RaycastHitbox:Initialize(Tool)
NewHitbox:DebugMode(true)

function onActivation()
	
NewHitbox:HitStart()
	
NewHitbox.OnHit:Connect(function(hit, humanoid)
	if hit.Parent.Name ~= character.Name then
	        humanoid:TakeDamage(10)
	        NewHitbox:HitStop()
		end
	end)
end

Tool.Activated:Connect(onActivation)

The rays do stop firing once they hit something, but once they get reactivated, there are “ghost rays” that also do damage. As seen here

https://gfycat.com/carefulserpentinefrenchbulldog

I really need some help to understand how to properly utilize this module.

1 Like

Don’t create the OnHit event connection in the activated function, this will cause a new connection every time the tool is used, thus a memory leak.

Leave it out the function.

1 Like

Okay, thank you for telling me about the memory leak and I’ve changed the script a bit.

Unfortunately I still have the problem of some Ghost Rays appearing after I reactivate the rays. Would you happen to know why this is happening and how to stop them?

Edited Script

local Tool = script.Parent
local player = Tool.Parent.Parent
local character = player.Character or player.CharacterAdded:Wait()
local RaycastHitbox = require(Tool.RaycastHitboxV2)


local NewHitbox = RaycastHitbox:Initialize(Tool,{character})
NewHitbox:DebugMode(true)

local Connection = nil
Connection = NewHitbox.OnHit:Connect(function(hit, humanoid)
	humanoid:TakeDamage(10)
	NewHitbox:HitStop()
	Connection = nil
end)


function onActivation()
	
NewHitbox:HitStart()

end

Tool.Activated:Connect(onActivation)

Ghost Rays I am talking about Roblox Capture

2 Likes