I don’t know if a feature like this has already been suggested but it would be nice if you could add a feature where rays are created in between two attachments.
For example, if I had 2 attachments on a sword and 1 attachment was on top and the other attachment was on the bottom of the sword, there would be rays created in between those two attachments with a variable that could decide how far each ray would be apart.
This is a great module nonetheless and I’m planning to implement it into one of my upcoming games.
I’m quite new to raycasting, so I’ve got a question (I haven’t seen this asked before). Do I need to put this script in a local script and then use a remote event to damage or do I cast the rays server side?
It’s personal preference. Put it on server side if you desire more security but less responsive gameplay, or put it on client side to have the ability to gain instant feedback (and make your combat feel a lot more snappier) in turn for more security checks (and generally a bit more work, using remote events, distance checks, angles, etc).
If you use the damage arguments instead of the OnHit connection line built into this module, then you will have to use it server sided.
Since the hit method is determined by casting a ray from the current attachment position to the previous one every frame, how would you safely do a distance check for that on the server?
Just a distance check probably won’t be enough, assuming you are using this client sided, and are using a remote event to tell the server “Hey, I damaged this enemy”. A potential exploiter can simply just call an entire different target across the map, and when the server receives this input from the clients, it should always check distance, be it magnitude or regions.
Recognize common sense in your game, such as an enemy should never be damaged 50 studs away if your swords are only 5 studs in length.
You can also combine distance check with angle checks. For example, to make sure you aren’t actually damaging someone close to you but directly below you.
These checks aren’t necessary if your hit methods are already all server sided and if that’s the case, efforts should be made to optimize it instead so your players can get the best responsiveness out of it. There can also be some potential exploits even on the server side, mostly teleportation or position based, so you should check for velocity or position differences here on top of any other checks.
I have implemented your hitbox module. I have done it for a sword and for a punch tool. The sword works fine, thank you, however the fists don’t seem to work good. They work for me, if I hit players in game I deal damage, but others players aren’t dealing any damage to anyone. This is very similar to a question I asked a week ago about touched events. Partly because that didn’t work I decided to try Raycasting, but now that doesn’t work for the punch tool again. When I enable debugmode then only my punch tool has red rays, others dont (I handle it server side), Below here is my script, only “Click” prints, and in the module script only “Created Hitbox for Object” prints. Maybe you can help me?
Kees
local tool = script.Parent
local player = tool.Parent.Parent
local character = player.Character or player.CharacterAdded:Wait()
local rightHandBox = Instance.new("Attachment",character.RightHand)
rightHandBox.Name = "DmgPoint"
local leftHandBox = Instance.new("Attachment",character.LeftHand)
leftHandBox.Name = "DmgPoint"
local RaycastHitboxModule = game.ServerScriptService.RaycastHitboxModule
local RaycastHitbox = require(RaycastHitboxModule)
local Damage = 10
local hitTime = 0.5 -- the time between each hit
local NewHitbox = RaycastHitbox:Initialize(character, {character})
NewHitbox.OnHit:Connect(function(hit, humanoid)
--- Do not put events on a loop, else you will memory leak and it will damage multiple times!!
print(hit.Parent.Name)
humanoid:TakeDamage(Damage)
end)
tool.Activated:Connect(function()
print("Click")
NewHitbox:HitStart()
wait(hitTime)
NewHitbox:HitStop()
end)
You can use ModuleScripts to get references to custom objects between scripts. Using the global environment for this is quite bad because it makes your code very hard to understand. I recommend not using _G nor shared at all in your code bases if you can – you don’t actually need it.
It was put in far before module scripts were. While _G and shared are not officially deprecated/removed, it’s still advised to use module scripts rather than storing everything in a shared table like that. Easier for other programmers to read a code base like this too if you collaborate. Since they can see the hierarchical tree structure of your code base.
Roblox doesn’t like to always remove features that are no longer necessary because by doing so they could inadvertently break a lot of games which still happen to rely on it.
Apologies for the late reply but in case if you still didnt fix it: if you dont see any red lines in the debug mode, there are a few possible solutions:
You initialized the wrong model. If its a tool, you should initialize the actual tool, not the character as the tool will unparent itself from the character when you unequip it.
Try V.1.21 if you dont have that. I fixed some weird tools compatibility on that version.
Initialization caches all points found made at the moment you call its function, so you cannot add or parent new ones until you deinitialize or use the setpoints function.
No this shouldnt be normal. It should be fine if its registering the hits but only damaging once.
Does this mean that I should make a handle and some parts and add attachments to these instead of the player character? (How would I make a tool also work for the left hand?)
Can’t answer the first one for you since I didn’t develop this (nor have I played around with it as much as I’d like to have yet), but I can the second one.
Left handed tools work the same as right handed tools, just that you have to add more work. In the case of using a tool instance or not, the concept is that you’re welding a tool to the hand and then setting up certain events on that left handed tool.
A while ago, I made a really quick tutorial for basic left/right hand tooling (it is also the method Dungeon Quest uses to equip tools, of you’re familiar with that game). See here:
This is a good concept to start with. Any other left hand tooling systems or implementations are up to you to craft depending on your needs. I’ve come up with a dozen tool systems at this point honestly, there’s so many ways to make them.
Im not sure how your tools are setup, but if im imagining it correctly, if the attachments are already in the character when you initialized it, it should theoretically work regardless if the script runs from a tool or not. But you can probably try that way instead as its cleaner.
I’ve got another question. Can I also use raycasting(And this module?) for firing projectiles and other ranged attacks? Or should I then use touched or something else instead?