Problem with Raycast Collision

Hey yall, so I have an issue with my Fireball ability which I never had before.
Whenever I throw my Fireball, it explodes instantly, when it’s supposed to explode when it hits something.

This is my script btw
	if skillName == "Fireball" then
		
		local hasExploded = false
		local isHit = false
		
		combatModule.PlayAnimation(humanoid, skillConfig:WaitForChild("AnimationID").Value)
		combatModule.CharacterFaceCursor(humanoidRootPart, mousePosition)
		humanoid.WalkSpeed = 0
		
		wait(0.5)
		
		humanoid.WalkSpeed = 20
		
		local fireball = spells:WaitForChild("Fireball"):Clone()
		fireball.Parent = vfx
		game:GetService("Debris"):AddItem(fireball, 5)
		fireball.CFrame = humanoidRootPart.CFrame + humanoidRootPart.CFrame.LookVector * 10
		
		local distance = (fireball.Position - mousePosition).Magnitude
		local speed = 0.2
		local tweenTime = speed * distance / 15
		
		local sound = sounds:WaitForChild(skillName):Clone()
		sound.Parent = fireball
		sound:Play()
		
		local rayOrigin = humanoidRootPart.Position
		local rayDirection = mousePosition
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
		local raycastParams = RaycastParams.new()
		raycastParams.FilterDescendantsInstances = {character, fireball}
		raycastParams.FilterType = Enum.RaycastFilterType.Exclude
		raycastParams.IgnoreWater = true
		local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
		
		local fireballTween = ts:Create(fireball, TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = raycastResult.Position})
		fireballTween:Play()
		
		local function ExplosionEffect()
			if not hasExploded then
				hasExploded = true
				fireballTween:Pause()
				combatModule.Debris(character, fireball.Position, 20, 5, Vector3.new(math.random(1,1.15), math.random(1,1.5), math.random(1,1.15)), 3, false, "Fire")
				rs:WaitForChild("Events"):WaitForChild("ShakeCamera"):FireAllClients(200, 0.1)
				ts:Create(fireball:WaitForChild("PointLight"), TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 0}):Play()
				ts:Create(fireball, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Size = fireball.Size + Vector3.new(2,2,2), Transparency = 1}):Play()
				for i, v in pairs(fireball:GetChildren()) do
					if v:IsA("ParticleEmitter") then
						v.Enabled = false
					end
				end
				for i, v in pairs(fireball:WaitForChild("Explosion"):GetChildren()) do
					if v:IsA("ParticleEmitter") then
						v:Emit(100)
					end
				end
				wait(0.5)
				fireball:Destroy()
			end

			if raycastResult then
				ExplosionEffect()
			else
				warn("No collision detected.")
			end
		end
		
		fireballTween.Completed:Connect(ExplosionEffect)
		
		fireball.Touched:Connect(function(hit)
			if hit.Parent.Name == character.Name then return end
			if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:WaitForChild("Shield").Value == false then
				if table.find(hits, hit.Parent) then return end
				if not table.find(hits, hit.Parent) then
					if not isHit then
						isHit = true
						ExplosionEffect()
						combatModule.Hit(player, hit, sounds:WaitForChild("Hit"), values:WaitForChild("DefaultDamage").Value * skillConfig:WaitForChild("DamageMulti").Value)
						table.insert(hits, hit.Parent)
					end
				end
			end
		end)
		
	end

Maybe it has something to do with this? What happens if you comment this out?

1 Like

Why do you divide it by 15? Maybe the time the tween lasts is to short.

1 Like

In the Touched event, have you tried just printing what it hit to see why it is doing that?

1 Like

That’s not the problem. That line is just to calculate the length of the tween using Magnitude

Alright everyone, so now it works but 2/3 chances for some reason.
Here’s the updated code.

Open
if skillName == "Fireball" then

	local hasExploded = false
	local isHit = false

	combatModule.PlayAnimation(humanoid, skillConfig:WaitForChild("AnimationID").Value)
	combatModule.CharacterFaceCursor(humanoidRootPart, mousePosition)
	humanoid.WalkSpeed = 0

	wait(0.5)

	humanoid.WalkSpeed = 20

	local fireball = spells:WaitForChild("Fireball"):Clone()
	fireball.Parent = vfx
	game:GetService("Debris"):AddItem(fireball, 5)
	fireball.CFrame = humanoidRootPart.CFrame + humanoidRootPart.CFrame.LookVector * 10
	
	local sound = sounds:WaitForChild(skillName):Clone()
	sound.Parent = fireball
	sound:Play()

	local rayOrigin = humanoidRootPart.Position
	local rayDirection = -(humanoidRootPart.Position - mousePosition)
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection)
	local raycastParams = RaycastParams.new()
	raycastParams.FilterDescendantsInstances = {character, vfx}
	raycastParams.FilterType = Enum.RaycastFilterType.Exclude
	raycastParams.IgnoreWater = true
	local raycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	
	local function ExplosionEffect(fireballTween)
		local sound = sounds:WaitForChild("Fireball_Explosion"):Clone()
		sound.Parent = fireball
		sound:Play()
		combatModule.Debris(character, fireball.Position, 20, 5, Vector3.new(math.random(1,1.15), math.random(1,1.5), math.random(1,1.15)), 3, false, "Fire")
		rs:WaitForChild("Events"):WaitForChild("ShakeCamera"):FireAllClients(500, 0.2)
		ts:Create(fireball:WaitForChild("PointLight"), TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Brightness = 0}):Play()
		ts:Create(fireball, TweenInfo.new(0.5, Enum.EasingStyle.Sine, Enum.EasingDirection.Out), {Size = fireball.Size + Vector3.new(2,2,2), Transparency = 1}):Play()
		for i, v in pairs(fireball:GetChildren()) do
			if v:IsA("ParticleEmitter") then
				v.Enabled = false
			end
		end
		for i, v in pairs(fireball:WaitForChild("Explosion"):GetChildren()) do
			if v:IsA("ParticleEmitter") then
				v:Emit(100)
			end
		end
		task.spawn(function()
			wait(2)
			fireball:Destroy()
		end)
	end
	
	if raycastResult then
		if not hasExploded then
			hasExploded = true
			local distance = (fireball.Position - raycastResult.Position).Magnitude
			local speed = 0.2
			local tweenTime = speed * distance / 15
			local fireballTween = ts:Create(fireball, TweenInfo.new(tweenTime, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut), {Position = raycastResult.Position})
			fireballTween:Play()
			fireballTween.Completed:Connect(function()
				ExplosionEffect()
				fireballTween:Pause()
			end)
		end
	else
		fireball:Destroy()
		warn("No collision detected.")
	end
	
	fireball.Touched:Connect(function(hit)
		if hit.Parent.Name == character.Name then return end
		if hit.Parent:FindFirstChild("Humanoid") and hit.Parent:WaitForChild("Shield").Value == false then
			if table.find(hits, hit.Parent) then return end
			if not table.find(hits, hit.Parent) then
				if not isHit then
					isHit = true
					ExplosionEffect()
					combatModule.Hit(player, hit, sounds:WaitForChild("Hit"), values:WaitForChild("DefaultDamage").Value * skillConfig:WaitForChild("DamageMulti").Value)
					table.insert(hits, hit.Parent)
				end
			end
		end
	end)
	
end