Raycast Hitbox 4.01: For all your melee needs!

Update 1:

I’m trying to use RaycastHitboxV4 in ServerScriptService but every example I’ve seen has been a script in the workspace.

Server Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")

local signals = ReplicatedStorage.Signals
local remotes = signals.Remotes

local raycastHitbox = require(script.RaycastHitboxV4)

local playerCooldowns = {}
local humanoidsHit = {}

local cooldown = 0.75--will be changed because each tool has different cooldowns
local damage = 25

local Attack = {}

Attack.Init = function()
	remotes.ToServer.OnServerEvent:Connect(function(player,signal,toolName)
		local char = player.Character
		local hum = char.Humanoid
		if signal == "Trigger" then
			
			local tool = char:FindFirstChild(toolName)
			if tool then
				if (playerCooldowns[player] and tick() - playerCooldowns[player] >= cooldown) or not playerCooldowns[player]  then
					playerCooldowns[player] = tick()
				else
					return
				end
				print("Successfully created")
				
				local hitbox = raycastHitbox.new(tool.Hitbox)
				local hitC = hitbox.OnHit:Connect(function(hit,humanoid)
					print(hit,humanoid)
					if humanoid == hum then return end
					humanoid:TakeDamage(damage)
				end)
				print("Started")
				hitbox:HitStart()
				task.wait(cooldown)
				hitbox:HitStop()
			end
		end
	end)
end

return Attack

When I click and attempt to fire this event to the server nothing happens but started and successfully created both print so I think its something wrong with how I’m using RaycastHitbox.

Can you only put this module and any script requiring it inside of a tool?

Update 2:

New Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local signals = ReplicatedStorage.Signals
local remotes = signals.Remotes

local raycastHitbox = require(script.RaycastHitboxV4)

local playerHitboxes = {}
local playerConnections = {}

local playerCooldowns = {}
local humanoidsHit = {}

local cooldown = 0.75--will be changed because each tool has different cooldowns
local damage = 25

local Attack = {}

Attack.Init = function()
	Players.PlayerAdded:Connect(function(plr)
		local hitbox

		plr.CharacterAdded:Connect(function(char)
			local hum = char.Humanoid
			char.ChildAdded:Connect(function(tool)
				
				if tool:IsA("Tool") and tool:FindFirstChild("Hitbox") then
					if not hitbox then hitbox = raycastHitbox.new(tool.Hitbox) end
					local c1 = tool.Activated:Connect(function()
						hitbox:HitStart()
						task.wait(cooldown)
						if hitbox then hitbox:HitStop() end
					end)
					local c2 = hitbox.OnHit:Connect(function(hit,humanoid)
						if humanoid == hum then return end
						humanoid:TakeDamage(damage)
					end)
					table.insert(playerConnections,c1)
					table.insert(playerConnections,c2)
				end
			end)
			char.ChildRemoved:Connect(function(tool)
				if tool:IsA("Tool") and tool:FindFirstChild("Hitbox") then
					for i,comp in ipairs(playerConnections) do
						comp:Disconnect()
						comp = nil
					end
					if hitbox then hitbox:Destroy() hitbox = nil end
				end
			end)
		end)
	end)
end

return Attack

So basically I removed firing to the server and made detection on the server.

There are multiple issues but my main one is that I can’t disconnect the OnHit event and I remember Swordphin saying he created something for the intended use of not disconnecting something so I’m not sure if I’m going in the right direction or not.

Update 3:

Ok all the fixes above I fixed but now I am back at bottom.

My current problem is that whenever I try to create a RaycastHitbox, the debug lines don’t show, the hitbox can’t be recognized in the code, and the hit detection doesn’t work.

I get random errors as well such as:

ServerStorage.Server.Attack.Melee:32: attempt to call missing method 'HitStop' of table

for _: number, point: Point in ipairs(ActiveHitboxes[i].HitboxRaycastPoints) do -- In the hitboxcaster

It mostly says HitStop and HitStart are missing methods.

It worked a while ago but I don’t know what I did to make it not work.

I checked through my past history but even in those past sessions when it did work, its not working anymore.

I don’t know what magical thing happened for this to change.

Code
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

local signals = ReplicatedStorage.Signals
local unreliables = signals.Unreliables

local raycastHitbox = require(script.RaycastHitboxV4)
local infoSignaler = require(script.Parent.InfoSignaler)

local playerHitboxes = {}

local function reset(connections)
	for _,comp in ipairs(connections) do
		comp:Disconnect()
	end
	table.clear(connections)
	return connections
end


local Melee = {}

Melee.Start = function(plr,tool,info,playerParams,plrConnections)
	local char = plr.Character or plr.CharacterAdded:Wait()
	local root = char.HumanoidRootPart
	local c1,c2
	playerHitboxes[plr] = raycastHitbox.new(tool.Hitbox)
	playerHitboxes[plr].RaycastParams = playerParams
	
	c1 = tool.Activated:Connect(function()
			playerHitboxes[plr]:HitStart()
		task.wait(info.Cooldown)
			playerHitboxes[plr]:HitStop()
	end)
	
	c2 = playerHitboxes[plr].OnHit:Connect(function(hit,humanoid)
		local targetChar = hit.Parent
		local targetRoot = targetChar.HumanoidRootPart

		for _,player in Players:GetPlayers() do
			local playerChar = player.Character or player.CharacterAdded:Wait()
			local playerRoot = playerChar.HumanoidRootPart
			if (playerRoot.Position - tool.Hitbox.Position).Magnitude < 5e2 then
				local returnedInfo = infoSignaler[tool.Name](tool)
				unreliables.ToClient:FireClient(player,tool.Name.."FX",returnedInfo)
			end
		end

		targetRoot.AssemblyLinearVelocity = ((targetRoot.Position - root.Position) * 25) + Vector3.new(0,50,0)
		humanoid:TakeDamage(info.Damage)
	end)
	
	table.insert(plrConnections,c1)
	table.insert(plrConnections,c2)
	return plrConnections
end

Melee.End = function(plr,plrConnections)
	if playerHitboxes[plr] then playerHitboxes[plr]:Destroy() table.clear(playerHitboxes[plr]) playerHitboxes[plr] = nil end
	local resetConnections = reset(plrConnections)
	return resetConnections
end

return Melee

I have another main module called attack above this one but it just starts and ends these when equipping and unequipping tools.

I also tried moving the playerHitboxes table to the parent attack module but it did the same thing.

Found an interesting bug(?).
This module only works on server-sided Scripts with the Legacy RunContext. The Server RunContext returns nil when performing RaycastHitbox.new().

I haven’t tested it with client-sided scripts, so I can’t report on that.


Edit: I realised you can’t create new hitboxes if the part isn’t parented to workspace. Since “Server” RunContext scripts run regardless of the container, that’s why I was getting my error.

Legends says you’re still doing awesome work on it :muscle:

those legends would be very right