Raycast Hitbox 4.01: For all your melee needs!

Can I send my code here? Soo I can know if I’m using the module wrongly

1 Like

I mean, sure, feel free to do so. You don’t need to ask me and I didn’t create this thread, I’m just someone who lurks here frequently.

1 Like

my code:

		local DmgPoints = RS.Specials.DmgPointsArms:GetChildren()
		for i = 1, #DmgPoints do
            local v = DmgPoints[i]
			local DmgPointClone = v:Clone()
            DmgPointClone.Parent = Character["Right Arm"]
		end
			
		local Animation = Instance.new("Animation")
		Animation.AnimationId = "rbxassetid://ANIMATIONID"
		local PunchAnimation = Character.Humanoid:LoadAnimation(Animation)

local Hitbox = RaycastHitbox:Initialize(Character, {Character})
Hitbox.OnHit:Connect(function(hit, humanoid)
--some stuff here
end)
		PunchAnimation:GetMarkerReachedSignal("StartDamaging"):Connect(function()
Hitbox:HitStart()
end)
		
		PunchAnimation:GetMarkerReachedSignal("StopDamaging"):Connect(function()
Hitbox:HitStop()
end)

		PunchAnimation.Stopped:Connect(function()
			Animation:Destroy()
			Animation2:Destroy()
			local CoroutineThread = coroutine.wrap(function()
			RaycastHitbox:Deinitialize(Character)
		    local DmgPoints = Character["Right Arm"]:GetChildren()
		    for i = 1, #DmgPoints do
                 local v = DmgPoints[i]
			     if v:IsA("Attachment") and v.Name == "DmgPoint" then
				     v:Destroy()
			     end
		    end
			end)
			CoroutineThread()
		end)

2 Likes

So I’ve tried working with this code in Studio and I can’t seem to find any issues. I’m not sure what issue you’re experiencing exactly here but for me, it’s seemed to have worked fine.

If you’re unable to discern a point of error, consider any of these three options:

  • Debug your code so that you can isolate the issue and resolve it.

    • Be sure to make use of the console. This module prints for various actions and you can do so as well.
  • Rework your code a bit and remake your punch tool from the ground up.

  • Consider creating a Scripting Support topic with the given resource, a repro file and remember to follow in the spirit of the category wrt the guidelines. They may be able to help you as well.

    • Quick issues with the module are fine to be taken up on a resource thread directly, but for longer issues that may not see any quick resolution, it’s worth creating a separate topic or reconvening with the resource author in private.
2 Likes

Hi i am on mobile so pardon if i missed anything but, may i ask why you are deleting the dmgpoints? Unless the animation is only played once ever, that maybe the cause of issue.

2 Likes

The animation should play everytime the player click at mouse, I think that can be the cause of the problem (lag)

4 Likes

I’m not using a tool, I’m adding attachments to player arm’s and it works fine, I was getting some lag in my game soo I think it might be because I’m creating a hitbox everytime player click at the mouse button.

2 Likes

Hey do you have a discord I can message you on? I would like to talk to you about further uses and current issues I’m experiencing.

2 Likes

Why not use this thread for that? It’s a perfect place to put brief issues and other use cases. This is also a public resource so your use cases or information may be able to help others, unless it’s for a confidential project you aren’t comfortable sharing details from.

6 Likes

Sorry it’s nothing like that, I just wanted a more comfortable place to talk and then I was planning to relay my information back to here in an organised manner.

1 Like

@TeamSwordphin,

Do you recommend this to be used on the Server or Client?

How to they compare and what are the differences other than Client-side would be smoother and better for UX but less secure;

How can we address in the best way possible to suit this module?

Is it designed to be used on the Client or Sever

3 Likes

It is honestly personal preference. This module was designed to be server sided at first (and I personally use it server sided cause my enemies use it) but if you require tight feedback such as PvP scenarios, client side might be a better option. As long as you do common sense checks, it can be secure as leaving it server sided (it just will need more work). Server side only has the downside of a little more latency. You will not be able to use the included damage function if you put it client sided, but most people don’t anyway. There is not much difference other than that.

You can also utilize this module both ways, having the client draw the hitboxes to ask if they can damage this target, and the server also drawing the hitboxes, making sure the client has accurately hit the right target. Putting it on the client helps you draw responsive visuals while also retaining accuracy that the server can then verify. I made this module in a way where people can adapt it to whatever scenario they please. Just go about it the same way you would approach any other hit detection system.

3 Likes

Is it accurate UX wise when implement both Server and Client Hit detection?

It’s terrible if the player sees that they hit a target but it doesn’t register.

What kind of checks do you recommend?

1 Like

Yea, if you are going to have both server/client simultaneous detection, you will need to make some sort of ping compensation. Alternative ways you can verify the hit is true from the client is angle checks. If the move is supposed to be a stab but you are getting damaged behind, that is a red flag. Moreover, A sword shouldn’t be able to hit a target 50 studs away. Raycast between your targets so they aren’t hitting each other through walls, etc. Use common sense in making server checks. Depending on what type of fighting game you are making, these server checks will vary heavily depending on your needs. There really isn’t one check that fits all games so you will have to do a bit of creative testing here.

PvP focused games usually favor the client, but some games do a hybrid system where lower pinged players will have their hit detection done on the client and simply sent to the server with verification and higher pinged players will have their hit detection on the server.

3 Likes

This is really nice. Will definitely use this!

2 Likes

So, I really liked this module and I pre-set attachments on tools for raycast hitboxing, however, I was stumbled on if you could or how to create those DmgPoint attachments via scripting? For example, if I wanted to create raycast hitboxing on a player’s fist, how would I do it? I was generally confused with a lot of the documentation inside the module.

1 Like

There are two ways to do this. The foolproof way is to just instance a new attachment object, rename it, and adjust its worldposition in your characters fist. After, call initialize (or Deinitialize first if you already initialized prior to attachment instancing). The module should automatically fetch it.

The second, slightly more advanced but preferable way if you are scripting dynamic dmg points are using the :SetPoints function. I had an example code somewhere in this thread where you can search for. This is good because you can make new points of raycast without deinitizializing your created hitboxes as it caches immediately upon calling the function. It also is probably more performance friendly too since it’s all vector based without the hassle of performance costs from instancing.

5 Likes

Hey Swordphin, do you know why the initial first attachments on each hands aren’t spawning correctly?
I gotta punch each fist once before every punch after those work as intended. This only happens the first time each fist has to punch.

https://gyazo.com/1a36dd6be7a397aebe8c900c6c7a5788.gif

Code:

local Hitbox = RaycastHitbox:Initialize(Character, {Character})


local function RightHandPoints(Hitbox)
	Hitbox:SetPoints(RightHand, {
		Vector3.new(0, 0, 0),
		Vector3.new(0, -0.1, 0),
		Vector3.new(0, 0.1, 0)
		}
	)
	Hitbox:SetPoints(RightLowerArm, {
		Vector3.new(0, 0, 0),
		Vector3.new(0, -0.1, 0),
		Vector3.new(0, 0.1, 0)
		}
	)
	local RightPunch = Character.Humanoid:LoadAnimation(RightPunch)
	RightPunch:Play()
end


local function LeftHandPoints(Hitbox)
	Hitbox:SetPoints(LeftHand, {
		Vector3.new(0, 0, 0),
		Vector3.new(0, -0.1, 0),
		Vector3.new(0, 0.1, 0)
		}
	)
	Hitbox:SetPoints(LeftLowerArm, {
		Vector3.new(0, 0, 0),
		Vector3.new(0, -0.1, 0),
		Vector3.new(0, 0.1, 0)
		}
	)
	local LeftPunch = Character.Humanoid:LoadAnimation(LeftPunch)
	LeftPunch:Play()
end

CombatRemote.OnServerEvent:Connect(function(Player, Info, Plr)
	if Player.Character.Properties.Move.Value and not MeleeDebounce and Player.Name == Plr.Name then
		Hitbox = RaycastHitbox:Initialize(Character, {Character})
		MeleeDebounce = true
		
		Hitbox.OnHit:Connect(function(Hit, Humanoid)
			if Humanoid.Health > 1 then
				local Move = Humanoid.Parent.Properties.Move.Value
				local HumanoidRootPart = Humanoid.Parent.HumanoidRootPart
				Humanoid.AutoRotate = false
				if Humanoid.Health - Damage > 1 then
					Humanoid:TakeDamage(Damage)
					Humanoid:MoveTo(HumanoidRootPart.Position + -4 * HumanoidRootPart.CFrame.lookVector)
					local Punched = Humanoid:LoadAnimation(Punched)
					Punched:Play()
				else
					Humanoid.Health = 0.01
					print("Dead")
				end
				Move = false
				wait(0.3)
				Move = true
				Humanoid.AutoRotate = true
			end
		end)
		
		local Animation
		local Hand
		
		if Info == "Right" then
			RightHandPoints(Hitbox)
			Animation = RightPunch
			Hand = RightHand
		
		elseif Info == "Left" then
			LeftHandPoints(Hitbox)
			Animation = LeftPunch
			Hand = LeftHand
		end
		Hitbox:HitStart()
		local Time = Animation.Length
		--delay(.3, function() Hitbox:HitStop() end)
		wait(Time)
		MeleeDebounce = false
		RaycastHitbox:Deinitialize(Character)
	end
end)
2 Likes

If I were to take a gamble, it is that animations on the server side (for it’s first time use) will take a little bit before it loads completely. Animation.Length may not be immediately available and will probably return 0 seconds which deinitializes the hitbox sooner than you expect. I had this happen when I was making AIs back in the days but feel free to prove me wrong with this theory. Try having the wait(Time) to be something longer just to see if I am correct.

Edit: To rectify this, I would recommend using this module client sided so the animations are loaded completely.

1 Like

Version 1.5 Beta

V.1.5 introduces some new HitboxObject functions:
HitboxObject:RemovePoints(Instance part, table vectorPoints)
HitboxObject:LinkAttachments(Instance attachment0, Instance attachment1)
HitboxObject:UnlinkAttachments(Instance attachment0)

RemovePoints works exactly the same way as SetPoints, except reverse. It removes all SetPoints with the same part instance and vector values you feed it. It will not do anything if the supplied vectors doesn’t exist for the given vector points in a hitbox.

Example code:

local RaycastHitbox = require(RaycastHitboxModule)
local NewHitbox = RaycastHitbox:Initialize(workspace.Brick)

NewHitbox:SetPoints(NewHitbox.Object,
     {
          Vector3.new(1, 0, 0),
          Vector3.new(0, 10, 50),
          Vector3.new(-40, 10, 3)
     }
)
NewHitbox:RemovePoints(NewHitbox.Object,
     {
          Vector3.new(1, 0, 0),
          Vector3.new(0, 10, 50),
          Vector3.new(-40, 10, 3)
     }
)

LinkAttachments requires two attachment objects (support for vectors at a later date, probably) which will raycast between those two points instead of one point.

---Example

NewHitbox:LinkAttachments(Character.Sword.HiltAttachment, Character.Sword.TipAttachment)

For LinkAttachments, it is meant for niche uses or in severe cases, to improve performance. It is not recommended to use this system for fast paced combat. As you can see in the above footage, it is no where near as accurate as the original raycastings, often missing me even if I was directly in front of it. However, you can combine this with the original system to make even weirder hitbox types.

You can remove links by using the first attachment.

NewHitbox:UnlinkAttachments(Character.Sword.HiltAttachment)
9 Likes