I created a fireball script and got some help to make it destroy instantly upon contact. However, there’s a glitch: when the fireball touches something, it passes through the part or character for a moment before being destroyed. Does anyone know how to fix this and thanks
local rem = script.Parent:WaitForChild("RemoteEvent")
local animationIds = {"97809031448069", "115238952410528"}
local currentAnimIndex = 1
local Debris = game:GetService("Debris")
local function playAnimation(player, animId)
local humanoid = player.Character:FindFirstChild("Humanoid")
if humanoid then
local animation = Instance.new("Animation")
animation.AnimationId = "rbxassetid://" .. animId
local track = humanoid:LoadAnimation(animation)
track.Priority = Enum.AnimationPriority.Action
track:Play()
return track
end
end
local function rotatePlayer(player, targetPosition)
local rootPart = player.Character:FindFirstChild("HumanoidRootPart")
local humanoid = player.Character:FindFirstChild("Humanoid")
if rootPart and humanoid then
if humanoid.MoveDirection.Magnitude < 0.1 then
local direction = Vector3.new(targetPosition.X - rootPart.Position.X, 0, targetPosition.Z - rootPart.Position.Z).unit
rootPart.CFrame = CFrame.new(rootPart.Position, rootPart.Position + direction)
end
end
end
local function enableParticles(arm)
local attachment = arm:FindFirstChild("E")
if attachment then
for _, particleName in ipairs({"Fire", "Main1", "Sparkles"}) do
local particle = attachment:FindFirstChild(particleName)
if particle then
particle.Enabled = true
end
end
task.delay(1, function()
for _, particleName in ipairs({"Fire", "Main1", "Sparkles"}) do
local particle = attachment:FindFirstChild(particleName)
if particle then
particle.Enabled = false
end
end
end)
end
end
rem.OnServerEvent:Connect(function(player, MousePosition)
rotatePlayer(player, MousePosition)
local track = playAnimation(player, animationIds[currentAnimIndex])
wait(0.1)
local shootFrom = currentAnimIndex == 1 and "Left Arm" or "Right Arm"
local arm = player.Character:FindFirstChild(shootFrom)
if arm then
enableParticles(arm)
local fb = game.ReplicatedStorage.FireBlast:Clone()
fb.Parent = workspace
fb.CFrame = arm.CFrame
-- Set network ownership to the player to reduce jitter
fb:SetNetworkOwner(player)
local Newfb = Instance.new("BodyForce")
Newfb.Force = Vector3.new(0, workspace.Gravity * fb:GetMass(), 0)
Newfb.Parent = fb
local direction = (MousePosition - fb.Position).unit
fb.Velocity = direction * 200
-- Use a variable to track if the fireball has already hit something
local hasHit = false
fb.Touched:Connect(function(hit)
-- Stop the fireball on first contact and execute effects
if not hasHit and not hit:IsDescendantOf(player.Character) then
hasHit = true -- Prevent further touches from being processed
-- Stop the fireball immediately
fb.Velocity = Vector3.new(0, 0, 0)
fb.Anchored = true
fb.A:Destroy()
fb.Attachment.E.Enabled = true
wait(0.1)
fb.Attachment.E.Enabled = false
-- Deal damage if it hits a player
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid:TakeDamage(50)
end
-- Destroy the fireball after 1 second
Debris:AddItem(fb, 1)
end
end)
end
-- Alternate between the two animations
currentAnimIndex = (currentAnimIndex % #animationIds) + 1
end)