How would I go about doing client-sided Hitboxes?
Would you like to use Roblox’s hit detection when a character’s body part is hit, or would you prefer a custom part that represents a hitbox on the character or its body parts, or an alternative method?
- Detect input on the client if necessary (M1s, Taps, etc)
- Fire this input to the server.
- Check if the client is eligible to create a hitbox (debounce, player.Character,)
- Create a
remotefunction
- Invoke the remotefunction into a localscript that creates a hitbox and returns the hits.
- Verify if the hits are within range using magnitude and optionally raycasts.
- Further, you may also want to create a server-sided hitbox to compare if the hits are also found on the server. Completely optional.
- Apply damage or other changes
- Ensure that all hitboxes are removed immediately before their hits are collected into a variable or returned to the server.
when the character hits i want a hitbox to spawn and then have it do damage
To achieve this, you would start by detecting if a character’s body part has been hit. If this is true, you would then create a hitbox for that specific body part or for the entire character, and proceed to apply damage based on your specific requirements.
ok ill try to do this thank you
ok im gonna try this thank you
Just note that you have to be careful as client sided hitboxes create as many problems as they solve, if done incorrectly.
Here’s an example to help you get started:
local Players = game:GetService("Players")
local Debris = game:GetService("Debris")
-- Define damage for each body part hitbox
local damageTable = {
Head = 50,
Torso = 30,
LeftArm = 20,
RightArm = 20,
LeftLeg = 25,
RightLeg = 25
}
-- Function to handle the damage application
local function onHitboxTouched(hitbox, hitPart)
local character = hitbox.Parent
local humanoid = character:FindFirstChildOfClass("Humanoid")
if humanoid then
-- Find which body part the hitbox is associated with
local hitboxName = hitbox.Name
local damage = damageTable[hitboxName]
if damage then
humanoid:TakeDamage(damage)
end
end
end
-- Create hitboxes and set up detection
local function setupHitboxes(character)
for _, part in ipairs(character:GetChildren()) do
if part:IsA("BasePart") and damageTable[part.Name] then
-- Create a hitbox
local hitbox = Instance.new("Part")
hitbox.Name = part.Name
hitbox.Size = part.Size
hitbox.CFrame = part.CFrame
hitbox.Anchored = true
hitbox.CanCollide = false
hitbox.Transparency = 1
hitbox.Parent = character
-- Connect the touched event
hitbox.Touched:Connect(function(hit)
if hit:IsA("BasePart") and hit.Parent:FindFirstChildOfClass("Humanoid") then
onHitboxTouched(hitbox, hit)
end
end)
-- Optional: Clean up hitboxes after a certain time
Debris:AddItem(hitbox, 5)
end
end
end
-- Connect to the player character added event
Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
-- Setup hitboxes for the character
setupHitboxes(character)
end)
end)
should i use server sided hit boxes or client sided because i heard that exploiters can mess with client sided hitboxes alot easier
This isn’t client-sided though
If you’re a newer developer use server sided only. I created a client-sided module (that included other features unique to that game) and reached 1K lines in a day. Unless you have the required experience, do not rush your progression into combat systems.
This can still be applied on the client-side, but I recommend using it on the server-side. You can use the hit function on the client and then fire a remote event to the server to apply damage.
Determining if the character or hitbox was hit on the client can help improve accuracy of detection.
Squid is right and if you’re a newer developer you probably want to stick with server-sided only hitboxes. Though be aware that if it is fully server-sided anyone with high ping will suffer since it may seem they hitted the target in their screen but for the server they didn’t. If you want to create a client-sided hitbox you probably want to make the hitbox client-sided and the damage server-sided (All of the properties need to be server-sided tho or else exploiters could abuse them)