# Increasing Ray Thickness & Making Ray Hit Multiple Targets

1st Problem [Solved]:

I made a script that damages nearby players using raycasting, the only problem is that the ray will only work when players are directly infront of you.

Example Video:

As you can see damage is only done when im directly facing the player, so how do I widen the raycast?

2nd Problem:

I want my damage script to hit multiple players at a time if they are also within its range, but the ray only hits the first target it finds.

Example Video:

How do I make a ray hit multiple targets instead of just one?

Damage Script (for both problems):

``````repeat wait() until script.Parent.Name == "HumanoidRootPart"

---- Character Vars

local char = script.Parent.Parent
local HRP = script.Parent
local RightHand = char:FindFirstChild("RightHand")

---- Find Weapon & DMG ----

local Weapon

for _,HandPart in pairs(RightHand:GetChildren()) do

if HandPart:FindFirstChild("SwordType") then
Weapon = HandPart
end

end

local MinDMG = Weapon:FindFirstChild("MinDMG")
local MaxDMG = Weapon:FindFirstChild("MaxDMG")
local AtkDMG = math.floor(math.random(MinDMG.Value,MaxDMG.Value))

---------------------------

---- Get ATK Range ----
local PlrSwordType = Weapon:FindFirstChild("SwordType")
local AtkRange

if PlrSwordType.Value == "Longsword" then

AtkRange = 7

elseif PlrSwordType.Value == "Greatsword" then

AtkRange = 7.8

elseif PlrSwordType.Value == "Spear" then

AtkRange = 11

end

---- DMG Raycast ----

local DamageRay = Ray.new(HRP.Position,HRP.CFrame.LookVector * AtkRange)

local hit,pos = workspace:FindPartOnRayWithIgnoreList(DamageRay, {script.Parent.Parent})

----------------------

if hit then

local hum = hit.Parent:FindFirstChild("Humanoid")
local SwordType = hit.Parent.Parent:FindFirstChild("SwordType")

-------- Do DMG to Shield --------

local Shield

if SwordType then

if SwordType.Value == "Shield" then

Shield = SwordType.Parent

local ShieldHealth = Shield:FindFirstChild("Health")

ShieldHealth.Value = ShieldHealth.Value - AtkDMG

end

end
----------------------------------

---- Do DMG to Opponent ----
if hum == nil and Shield == nil then

hum = hit.Parent.Parent:FindFirstChild("Humanoid")

if hum == nil then

hum = hit.Parent.Parent.Parent:FindFirstChild("Humanoid")

if hum == nil then

hum = hit.Parent.Parent.Parent.Parent:FindFirstChild("Humanoid")

else

hum:TakeDamage(AtkDMG)

end

else

hum:TakeDamage(AtkDMG)

end

elseif hum ~= nil and Shield == nil then

hum:TakeDamage(AtkDMG)

end
end
``````

you need to cats multiple rays,
a fast and easy solution can be

2 Likes

Hey so I figured out how to make the weapon hit multiple targets, but is there a way to make the Raycast wider without using the hitbox module?

Just use touch events for melee weapons, and have a table of humans hited to prevent same target getting damaged twice

instead of firing a raycast, you could instance a new part and set it to how big you want the hitbox to be, position it in front of the character then use BasePart:GetTouchingParts() which will return a table of all the baseparts which are touching the hitbox part. then you can go through the table in a for loop and check if each part is called â€śHumanoidRootPartâ€ť, if it is called that then you can find the humanoid and damage it

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.