Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

(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

✓ Accepted answer

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)

This discussion is preserved as it was. New answers, comments, and votes are closed.