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