Cannonball keeps exploding early, gets logged as "touched"

Any help would be greatly appreciated

local barrel = script.Parent
local fired = script.Parent.fired

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local CannonBall = ReplicatedStorage:WaitForChild("CannonBall")
local Explosion = ReplicatedStorage:WaitForChild("Explosion")

local cooldown = 7
local speed = 100
local exploded = false

local function fire()
	exploded = false
	local CannonBallClone = CannonBall:Clone()
	CannonBallClone.Parent = game.Workspace
	CannonBallClone.Position = barrel.Position
	fired:Play()
	CannonBallClone.AssemblyLinearVelocity = barrel.CFrame.LookVector * speed
	local function explode()
		local Explosion = Instance.new("Explosion")
		Explosion.Parent = game.Workspace
		Explosion.Position = CannonBallClone.Position
		wait(0.1)
		CannonBallClone:Destroy()
	end
	CannonBallClone.Touched:Connect(function(hit)
		print("Touched!")
		if exploded == false then
			explode()
			exploded = true
		end
	end)
	wait(cooldown) -- Explodes after a certain time if cannon ball is untouched
	if exploded == false then
		explode()
		exploded = true
	end
end

while wait(1) do
	fire()
end
2 Likes

It doesn’t look like anything is wrong, just that the projectile is obeying gravity. (You don’t have a force applied to the cannonball that counteracts gravity)
If you want your cannonball to have gravity, maybe tilt it up like 45 degrees upwards

1 Like

I would use TweenService to move the cannonball towards where you want it to go. It’s more adjustable and smoother. You’ll be able to anchor the cannonball and still have it move.

Most likely, your problem is that the cannonball may be touching itself (if you have other parts inside it), or the cannonball launcher. Use a whitelist system to make sure the cannonball is not touching itself or the cannon.

CannonBallClone.Touched:Connect(function(hit)
	if exploded == false and hit == "Baseplate" then --modify 
		explode()
		exploded = true
	end
end)
1 Like

This appears to be the issue because when I disabled the exploding and destroy function, it hit the ground around that spot. How do I add a force that contracts gravity?

bump


bump


You should give this a read!

1 Like

Which property do I use to make it unaffected by gravity though?

You can use a VectorForce to apply a force counteracting gravity.

Make sure to apply the force relative to the world.

However, there’s a much easier (and probably more sensible) option:

1 Like

How would I go about adding this into my script? Do I need attachments, etc.?

It’s as simple as adding an attachment to a BasePart and then applying the VectorForce to that attachment.

The VectorForce.Attachment0 must be set to the BasePart’s attachment.

If you’re already cloning a BasePart to spawn it into the world, you can just add the attachment and force to that instead.

1 Like

This fixed my problem, thank you

1 Like