Need Help with CFraming in a Gun

This function is supposed to produce a bullet fired from a plane’s machine gun when fired.

It fires, but the bullet spawns too close to the plane, and has a high chance of breaking the place, despite some if statements I have to account for it.

Here’s the function:

function FireBullet(Player,Shoot)
	if Shoot then
		local Part = Instance.new("Part")
		Part.BrickColor = BrickColor.new("Bright yellow")
		Part.Name = "Bullet"
		Part.CanCollide = false
		Part.FormFactor = "Symmetric"
		Part.Size = Vector3.new(5,5,3)
		Part.BottomSurface = "Smooth"
		Part.TopSurface = "Smooth"
		local Mesh = Instance.new("BlockMesh")
		Mesh.Parent = Part
		Mesh.Scale = Vector3.new(1/25,1/25,1)
		local BV = Instance.new("BodyVelocity")
		BV.Parent = Part
		BV.maxForce = Vector3.new(math.huge,math.huge,math.huge)
		local PlaneTag = Instance.new("ObjectValue")
		PlaneTag.Parent = Part
		PlaneTag.Name = "PlaneTag"
		PlaneTag.Value = Vehicle
		Part.CFrame = Shoot.CFrame * CFrame.new(0, 0, -55) 
		BV.velocity = (Engine.BodyVelocity.Velocity) + (Part.CFrame.lookVector * 2000) + (Vector3.new(0,0.15,0))
		Part.Parent = game.Workspace
		Part.Touched:connect(function(Object) --This lets me call the "Touched" function, which means I don't need an external script
			if (not Object:IsDescendantOf(Chara)) and (not Object:IsDescendantOf(Vehicle)) then
				local HitHumanoid = Object.Parent:findFirstChild("Humanoid")
				if HitHumanoid~=nil then
					HitHumanoid:TakeDamage(100) --This only damges the player if they don't have a forcefield
					local CreatorTag = Instance.new("ObjectValue")
					CreatorTag.Name = "creator"
					CreatorTag.Value = Player
					CreatorTag.Parent = HitHumanoid
				else
					Object:BreakJoints()
				end
			end
		end)
		delay(3,function() Part:Destroy() end)
	end
end
1 Like

Can you show us where you are defining vehicle?

local Vehicle = script.Parent

The script is within the immediate jet model.

Sounds like you have encountered lag!
With your plane, you likely have the client controlling it and running physics for it, otherwise it would be too unresponsive to actually fly well. The server has authority over the network, but takes time to reply. To fix your bullet issue, you want to create a bullet on the client and do detection that way. This requires more remote events, such as one for when the bullet is created, one when it is hit, and one to replicate the bullets made by other players. This may sound insecure, and it is, however it’s just as secure as .Touched events, as those rely on client detection. You can change this with modules that raycast, such as fastcast, and remedy the insecurities with server sided verification of hits, to make sure they aren’t being changed by exploiters.

1 Like

Yes, this will work, however he did put this line in there as an easier fix.

Part.Touched:connect(function(Object) 
			if (not Object:IsDescendantOf(Chara)) and (not Object:IsDescendantOf(Vehicle)) then

I personally don’t see why this doesn’t work.

Just so we’re clear:
All of the jet parts are underneath Vehicle, and if you were to print(Object:IsDecendantOf(Vehicle)) It still returns false?

I found out that the bullets produced would hit each other, due to the lag that @Styre_x mentioned.

I fixed that issue and combined it with setting Network ownership to the pilot, so the lag isn’t as bad. However, I will probably have to switch to a client/server sided model for the bullets sometime soon.