So i have this code, which lets me fire cannonBalls whereever my mouse is located to. However if shoot at the sky, no cannonBall gets shooted:
local Debris = game:GetService("Debris")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CollectionService = game:GetService("CollectionService")
local SoundService = game:GetService("SoundService")
local TweenService = game:GetService("TweenService")
local shootEffectOffset = 2
local MousePositionEvent = ReplicatedStorage:WaitForChild("MousePositionEvent")
local function fireCannon(cannon, targetPosition)
local firingPosition = cannon.Position + (cannon.CFrame.LookVector * shootEffectOffset)
local shootEffect = Instance.new("Explosion")
shootEffect.Position = firingPosition
shootEffect.ExplosionType = Enum.ExplosionType.NoCraters
shootEffect.BlastPressure = 0
shootEffect.Parent = cannon
Debris:AddItem(shootEffect, 0.2)
SoundService.FireSound:Play()
local clonedBall = ReplicatedStorage:FindFirstChild("CannonBall"):Clone()
clonedBall.Position = firingPosition
clonedBall.Parent = workspace.CannonBalls
local heightDifference = targetPosition.Y - firingPosition.Y
local midpoint = (firingPosition + targetPosition) / 2
midpoint = Vector3.new(midpoint.X, midpoint.Y + heightDifference / 2, midpoint.Z)
local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local goal = {}
goal.Position = targetPosition
local tween = TweenService:Create(clonedBall, tweenInfo, goal)
tween.Completed:Connect(function(status)
if status == Enum.PlaybackState.Completed then
clonedBall:Destroy()
end
end)
tween:Play()
clonedBall.Touched:Connect(function(hit)
local humanoid = hit.Parent:FindFirstChild("Humanoid")
if humanoid then
humanoid.Health = 0
end
end)
Debris:AddItem(clonedBall, 1.25)
end
MousePositionEvent.OnServerEvent:Connect(function(player, targetPosition)
for _, cannon in CollectionService:GetTagged("Cannon") do
fireCannon(cannon, targetPosition)
end
end)