I've made a cannon in Roblox, it uses part velocity to launch cannon ball projectiles out the cannon. It attaches a function to the touched event for the cannon ball that's taken from server storage and moved to workspace. The cannon ball when touched and the part touched is not part of the cannon itself generates an explosion.
The problem is that it works for roughly 20 shots, its a different amount of successful cannon shots each time however after a few the cannon ball's position starts return incorrectly and as a result the explosions explode in mid air.
To figure out the root cause of this I've tried removing the explosion and making it spawn a part there instead and after roughly 20 shots it will start to make the parts appear in mid air instead of where the cannon ball actually collided. I've tried both making the cannon ball collide and non collide which made no difference. It appears that the position within the touched event function is not returning the true position of where the part was touched in 3D space.
local function cannonMC1CTSEvent(player, mousePos)
local cannonBall = game.ServerStorage.cannonball:Clone()
cannonBall.Position = script.Parent.barrel.CFrame.Position
cannonBall.Parent = workspace
physicsService:SetPartCollisionGroup(cannonBall, "cannonBall")
script.Parent.barrel.CFrame = CFrame.new(script.Parent.barrel.CFrame.Position, mousePos)
cannonBall.CFrame = CFrame.new(script.Parent.barrel.CFrame.Position, mousePos)
cannonBall.Velocity = cannonBall.CFrame.LookVector * 256
local function generateExplosion()
explosion = Instance.new("Explosion")
explosion.BlastRadius = 2
explosion.BlastPressure = 100
end
generateExplosion()
cannonBall.Touched:Connect(function(touched)
if touched.Parent.Name ~= "cannon" then
explosion.Position = cannonBall.Position
cannonBall:Destroy()
explosion.Parent = workspace
generateExplosion()
end
end)
Here is the code for the cannon ball, the code fires a cannon ball every time the user clicks which is done locally using a remote event to tell server to fire the cannon. I've been baffled with this issue and my first guess would be that it's just Roblox Part.Velocity being buggy. Thankyou in advance!
I've spent about 5 hours today trying to find the root of the issue. It's the Roblox touched event. It is inaccurate at detecting collision of moving parts. I figured out this was the issue by checking whether ArePartsTouchingOthers was true or false, the times the explosion was in mid air and inaccurate to the visual position of the cannonball when it collides was false whereas when it was correctly positioned it was true. Therefore I am going to remake my code around using ArePartsTouchingOthers instead of using the touched event.