Hello, I am trying to implement splash damage to a tower in my tower defense game, I have no idea how to do this and I will give you the code to my tower. Thanks
Script:
local idle = script.Idle
local attack = script.Attack1
local enabled = false
local sound = script.Explosion
local reload = script.Reload
local statsF = script.Parent.Stats
local loadIdle = script.Parent.Humanoid:LoadAnimation(idle)
local loadAttack = script.Parent.Humanoid:LoadAnimation(attack)
loadIdle:Play()
function attack1(units, i)
loadAttack:Play()
loadIdle:Stop()
local NewCFrame = CFrame.new(script.Parent.HumanoidRootPart.Position, units.HumanoidRootPart.Position)
script.Parent:SetPrimaryPartCFrame(NewCFrame)
local x = game:GetService("ReplicatedStorage"):WaitForChild("Rocket"):Clone()
x.Parent = workspace
x.CFrame = script.Parent.HumanoidRootPart.CFrame * CFrame.new(0.75,0.75, 0)
x.Anchored = true
wait(.2)
x.Anchored = false
script.Parent.RocketLauncher.Rocket.Transparency = 1
local bp = Instance.new("BodyPosition", x)
bp.MaxForce = Vector3.new(math.huge, math.huge, math.huge)
bp.Position = units.HumanoidRootPart.Position
bp.P = 17500
wait(.3)
script.Parent.RocketLauncher.Back.Smoke.Enabled = true
local dmg = statsF.Damage.Value
local distance = (units.HumanoidRootPart.Position - units.HumanoidRootPart.Position).magnitude
if distance < 5 then
units.Humanoid:TakeDamage(dmg)
units.Humanoid.Died:Connect(function()
units:Destroy()
end)
end
wait(0.16)
x:Destroy()
local exp = Instance.new("Explosion", workspace)
exp.Name = "Explosion"
exp.Position = x.Position
exp.DestroyJointRadiusPercent = 0
exp.BlastPressure = 0
sound:Play()
x:Destroy()
wait(1.8)
script.Parent.Rocket.Transparency = 0
script.Parent.RocketLauncher.Back.Smoke.Enabled = true
exp:Destroy()
sound:Stop()
wait(.5)
reload:Play()
wait(.5)
reload:Stop()
script.Parent.Rocket.Transparency = 1
script.Parent.RocketLauncher.Rocket.Transparency = 0
loadAttack:Stop()
loadIdle:Play()
end
while wait() do
if enabled == false then
enabled = true
local attacked = false
for i, units in pairs(workspace.TD.Minions:GetChildren()) do
local distance = (units.HumanoidRootPart.Position - script.Parent.HumanoidRootPart.Position).magnitude
if distance < 15 and attacked == false then
attacked = true
if script.Parent.Stats.Level.Value >= 1 then
attack1(units, i)
end
end
end
end
wait(script.Parent.Stats.AttackPerSecond.Value)
enabled = false
attacked = false
end