Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I prevent my fireball from breaking the script when it touches the player?

Asked by 4 years ago
Edited 4 years ago

(My problem is that when the fireball touches the Player it doesn't work any more for the enemy which leads to not damaging the enemy.)


local Player = game.Players.LocalPlayer local mouse = Player:GetMouse() local Character = Player.Character local CoolDown = true mouse.KeyDown:connect(function(key) if key == "f" then local Fireball = Instance.new("Part") local Fire = Instance.new("Fire") local Velocity = Instance.new("BodyVelocity") Fireball.Name = "Fire" Fireball.Material = "Neon" Fireball.BrickColor = BrickColor.new("Neon orange") Fireball.Parent = workspace Fire.Parent = Fireball Fireball.CFrame = Player.Character.UpperTorso.CFrame * CFrame.new(2,1,-1) Fireball.Size = Vector3.new(1.5,1.5,1.5) Velocity.Parent = Fireball Fireball.Shape = "Ball" Velocity.Velocity = Player.Character.UpperTorso.CFrame.LookVector * 35 Fireball.Touched:Connect(function(hit) if hit.Parent then if CoolDown == true then wait(0.02) CoolDown = false Fireball:Destroy() Fire:Destroy() local Enemy = hit.Parent:FindFirstChild("Enemy") Enemy.Health = Enemy.Health - 10 wait(1) CoolDown = true end end end) end end)

1 answer

Log in to vote
1
Answered by 4 years ago

Rather than destroying the fireball right after it hits hit.Parent, try destroying it after it hits the enemy. Also, you should find the humanoid if you are looking to deal damage.

local Player = game.Players.LocalPlayer
local mouse = Player:GetMouse()
local Character = Player.Character
local CoolDown = true

mouse.KeyDown:connect(function(key)
    if key == "f" then
        local Fireball = Instance.new("Part")
        local Fire = Instance.new("Fire")
        local Velocity = Instance.new("BodyVelocity")
                Fireball.Name = "Fire"
                Fireball.Material = "Neon"
                Fireball.BrickColor = BrickColor.new("Neon orange")
                Fireball.Parent = workspace
                Fire.Parent = Fireball
                Fireball.CFrame = Player.Character.UpperTorso.CFrame * CFrame.new(2,1,-1)
                Fireball.Size = Vector3.new(1.5,1.5,1.5)
                Velocity.Parent = Fireball
                Fireball.Shape = "Ball"
                Velocity.Velocity = Player.Character.UpperTorso.CFrame.LookVector * 35
                Fireball.Touched:Connect(function(hit)
            if hit.Parent then
                if CoolDown == true then
                    wait(0.02)
                    CoolDown = false
                                local Enemy = hit.Parent:FindFirstChild("Humanoid") --You should search for humanoid, not enemy, if you want to deal damage.
                                Enemy.Health = Enemy.Health - 10
                                wait(1)
                                        CoolDown = true
                    Fireball:Destroy() --Try destroying the fireball after its hit the enemy
                                Fire:Destroy()
                                end
                        end
                end)
        end
end)
Ad

Answer this question