I’m making a punch script using raycasting.
Currently, it only does damage if the enemy is touching the single Ray. How can I make it so it deals damage to enemies around that ray? Also, it only does damage to only one enemy at a time, how can I damage multiple enemies at a time?
I’m new to roblox lua.
local Enabled = true
local Combo = 1
wait(.001)
script.Parent.OnServerEvent:connect(function(Player, Action, V1)
local c = Player.Character
if Enabled == false then return end
if Action == "Combat" then
Enabled = false
if Combo == 1 then
Combo = 2
local Track = script.Punch1
local Anim = c.Humanoid:LoadAnimation(Track)
Anim:Play()
script.Event:Fire()
elseif Combo == 2 then
Combo = 1
local Track = script.Punch2
local Anim = c.Humanoid:LoadAnimation(Track)
Anim:Play()
script.Event:Fire()
end
wait(.3)
local R = Ray.new(c.HumanoidRootPart.Position, c.HumanoidRootPart.CFrame.lookVector*6) -- ray <<
local v, endp = workspace:FindPartOnRay(R, c)
if v ~= nil then
if v.Parent:findFirstChild("Humanoid") and v.Parent:findFirstChild("Deb") == nil and v.Parent ~= c then
v.Parent.Humanoid:TakeDamage(4) -- deal damage <<
local Deb = Instance.new("BoolValue", v.Parent)
Deb.Name = "Deb"
game.Debris:AddItem(Deb,0.2)
local S = Instance.new("Sound", v)
S.SoundId = "rbxassetid://131237241"
S.PlaybackSpeed = math.random(80,120)/100
S:Play()
local FX = Instance.new("Part", workspace.FX)
FX.Name = "CombatHit"
FX.Transparency = 0
FX.CanCollide = false
FX.Anchored = true
FX.Material = "ForceField"
FX.Color = Color3.new(1,0,0)
FX.Size = Vector3.new(1,1,1)
local SM = Instance.new("SpecialMesh", FX)SM.MeshType = "Sphere"
SM.Scale = Vector3.new(0,0,0)
FX.CFrame = v.Parent.HumanoidRootPart.CFrame*CFrame.new(math.random(-20,20)/10,math.random(-20,20)/10,math.random(-20,20)/10)
local BV = Instance.new("BodyVelocity", v)
BV.maxForce = Vector3.new(25000,25000,25000)
BV.Velocity = c.HumanoidRootPart.CFrame.lookVector*5
game.Debris:AddItem(BV,0.2)
end
end
wait(0.15)
Enabled = true
if script.Parent.Parent.Parent.Humanoid.Health < 1 then
script.Parent.Parent:Destroy()
end
end
end)