So im trying to add hitboxes to my game but they are not that great. i have parts welded to the player so they follow the player and this is my raycasting code to detect what the player shoots at in my fps ` local ReplicatedStorage = game:GetService(“ReplicatedStorage”)
local Players = game:GetService(“Players”)
local Teams = game:GetService(“Teams”)
local FireEvent = ReplicatedStorage:WaitForChild(“FireEvent”)
local CanShoot = ReplicatedStorage:WaitForChild(“CanShoot”)
FireEvent.OnServerEvent:Connect(function (player,BarrelPos,MousePos,Damage,HeadShotDamage)
local ray = Ray.new(BarrelPos,(MousePos - BarrelPos).unit * 99999999)
local part = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
if part then
local humanoid = part.Parent:FindFirstChild("Humanoid")
local Enemy = game.Players:GetPlayerFromCharacter(part.Parent)
if humanoid and humanoid.Parent.Name:lower() == " " then
if humanoid and not humanoid:IsDescendantOf(player.Character) then
if part.Name:lower() == "head" then humanoid:TakeDamage(HeadShotDamage) else humanoid:TakeDamage(Damage) end
end
end
if Enemy then
local tag = Instance.new("ObjectValue")
tag.Value = player -- this can either be the shooters character or plr object (adjust accordingly if you use character)
tag.Name = "creator"
tag.Parent = Enemy.Character.Humanoid -- this is the enemies humanoid, again can be parented to their character (adjust to it if you use character)
game:GetService("Debris"):AddItem(tag,.5) -- destroys tag after half a second
end
if CanShoot.Value == true then
if humanoid and not humanoid:IsDescendantOf(player.Character) then
if part.Name:lower() == "headhitbox" or part.Name:lower() == "head" then
humanoid:TakeDamage(HeadShotDamage)
print("head shot")
elseif part.Name:lower() == "bodyhitbox" or not part.Name:lower() == "head" then
humanoid:TakeDamage(Damage)
print("body shot")
end
end
end
end
end)` im not sure what im missing and how to make it respond better. i know that raycast hitbox thing exists but i have no idea how to get it working for something like this. because the github doesnt have anything like this. any help would be great. i dont know how other games tackle with hitboxes and im not sure how they make it responsive