How to make fireball disappear when shot at nothing after some time using tween service?

-- Local Script

local Fireball_Tool = script.Parent

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local Mouse = player:GetMouse()

local HRP = character:WaitForChild("HumanoidRootPart")

local Remote = script:WaitForChild("RemoteEvent")

local MAX_RANGE = 200

Fireball_Tool.Activated:Connect(function()
	local RaycastParameters = RaycastParams.new()
	RaycastParameters.FilterType = Enum.RaycastFilterType.Exclude
	RaycastParameters.FilterDescendantsInstances = {character, Fireball_Tool}
	RaycastParameters.IgnoreWater = true
	
	local Origin = (HRP.CFrame * CFrame.new(0, 0, -5)).Position -- The ball is in front of the HRP using RELATIVE not to be confused with ABSOLUTE position
	local Direction = ((Mouse.Hit.Position - Origin).Unit) * MAX_RANGE
	local RayResults = game.Workspace:Raycast(Origin, Direction, RaycastParameters)
	
	if not RayResults then
		Remote:FireServer(Origin, Mouse.Hit.Position, ((Mouse.Hit.Position - Origin).Magnitude), false)
	else
		Remote:FireServer(Origin, RayResults.Position, RayResults.Distance, true)
	end
end)
-- Server Script
local Debris = game:GetService("Debris")
local TweenService = game:GetService("TweenService")

local Remote = script.Parent

local function Create_Fireball(Origin, End_Position)
	local Fireball = Instance.new("Part")
	Fireball.Name = "Fireball"
	Fireball.Shape = Enum.PartType.Ball
	Fireball.Anchored = true
	Fireball.CastShadow = false
	Fireball.CanCollide = false
	Fireball.Size = Vector3.new(2, 2, 2)
	Fireball.Material = Enum.Material.Neon
	Fireball.Color = Color3.fromRGB(245, 128, 25)
	Fireball.Position = Origin
	Fireball.CFrame = CFrame.lookAt(Origin, End_Position)

	local Attachment = Instance.new("Attachment")
	Attachment.Orientation = Vector3.new(90, 0, 0)
	Attachment.Parent = Fireball

	local Fire = Instance.new("Fire")
	Fire.Heat = 20
	Fire.Size = 8
	Fire.Parent = Attachment

	Fireball.Parent = game.Workspace
	
	return Fireball
end

local function Explode_Fireball(player, Fireball)
	local Explosion = Instance.new("Part")
	Explosion.Name = "Fireball_Explosion"
	Explosion.Shape = Enum.PartType.Ball
	Explosion.Color = Color3.fromRGB(255, 120, 0)
	Explosion.Material = Enum.Material.Neon
	Explosion.Transparency = 0.6
	Explosion.Anchored = true
	Explosion.CanCollide = false
	Explosion.Position = Fireball.Position
	Explosion.Size = Vector3.new(1, 1, 1)
	Explosion.Parent = game.Workspace

	Explosion.Touched:Once(function(hit)
		print("EXPLOSION")
		print(hit.Name)
		if not hit.Parent:FindFirstChild("Humanoid") or hit.Parent:FindFirstChild("ForceField") or hit.Parent.Name == player.Name then return end

		local Humanoid = hit.Parent:FindFirstChild("Humanoid")
		local HRP = hit.Parent:FindFirstChild("HumanoidRootPart")
		Humanoid:TakeDamage(15)

		local Fire = Instance.new("Fire")
		Fire.Parent = HRP

		for i = 0, 3, 1 do
			if i >= 3 or Humanoid.Health <= 0 then Fire:Destroy() break end
			Humanoid:TakeDamage(5)
			print("Take 5 damage")
			task.wait(1)
		end
	end)
	
	return Explosion
end

Remote.OnServerEvent:Connect(function(player, Origin, End_Position, Distance, Valid_Shot)
	local Fireball = Create_Fireball(Origin, End_Position)
	
	local Tween_Position = TweenService:Create(Fireball, TweenInfo.new(0.25 * (Distance / 25), Enum.EasingStyle.Linear), {Position = End_Position})
	Tween_Position:Play()
	
	Fireball.Touched:Once(function(hit)
		print("Fireball")
		print(hit.Name)
		
		if Tween_Position.PlaybackState == Enum.PlaybackState.Playing then
			Tween_Position:Cancel()
		elseif Tween_Position.PlaybackState == Enum.PlaybackState.Completed then
			return
		end
		
		task.spawn(function()
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent.Name ~= player.Name and not hit.Parent:FindFirstChild("ForceField") then
				local Humanoid = hit.Parent:FindFirstChild("Humanoid")
				Humanoid:TakeDamage(5)
			end
		end)
		
		local Explosion = Explode_Fireball(player, Fireball)
		
		local Explode = TweenService:Create(Explosion, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Size = Vector3.new(20, 20, 20)})
		Explode:Play()
		Explode.Completed:Wait()
		
		Fireball:Destroy()
		Explosion:Destroy()
	end)
	
	Tween_Position.Completed:Wait()
	
	local Explosion = Explode_Fireball(player, Fireball)
	
	local Explode = TweenService:Create(Explosion, TweenInfo.new(0.1, Enum.EasingStyle.Linear), {Size = Vector3.new(20, 20, 20)})
	Explode:Play()
	Explode.Completed:Wait()

	Fireball:Destroy()
	Explosion:Destroy()
end)

I’m almost done with my Fireball script and I’m trying to perfect it but I don’t know how to make the fireball get destroyed after some time when it got shot at literally nothing. I don’t know how to modify my script to do it.

Debris is probably what you’re looking for

I have debris but I don’t know where to put it in my script to make it work

I was thinking of putting it right after my Tween_Position:Play() but then it will destroy it no matter what

Ah I think I know what you’re looking for, you want to be able to cancel the destruction if necessary.


You can use the task library for this.
More specifically, task.delay and task.cancel.

task.delay returns a thread (not an actual thread, just simulated), which can be prevented from resuming when passed to a call to task.cancel.

1 Like

Oh yeah! :smiley: but uhhh I don’t know how to use it ;-; I heard of it though :thinking:

It’s pretty simple!

Here’s an example:

-- Schedule a function to be resumed 5 seconds from now
local delayedTask = task.delay(5, function()
	print("Hello world!")
end)

-- Nevermind, cancel the task!
task.cancel(delayedTask)

Oh wow! Thanks! It works now! :smiley: