Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why doesn't my bullet fly straight? *Un-answered*

When I click, the bullet doesn't go straight. Instead, it comes out different ways. I don't know why it does this. Help?

local tool = script.Parent
local reloading = script.Parent.Reloading
local autofire = false

function fireBullet(d)
	local bullet = Instance.new("Part")
	bullet.FormFactor = "Custom"
	bullet.Size = Vector3.new(1.5, 0.35, 0.35)
	bullet.BrickColor = BrickColor.new("White")
	bullet.Position = tool.Handle.Position + (d * 5)
	bullet.Velocity = d * 125
	
	local s = script.RemoveBullet:Clone()
	s.Disabled = false
	s.Parent = bullet
	
	local force = Instance.new("BodyForce")
	force.force = Vector3.new(0, 25, 0)
	force.Parent = bullet
	
	bullet.Parent = game.Workspace
end

function semiFire(d)
	fireBullet(d)
end

function burstFire(d)
	for i = 1, 3 do
		fireBullet(d)
		wait(0.15)
	end
end

function autoFire(hum, chr)
	while autofire do
		local targetPos = hum.TargetPoint
		local lookAt = (targetPos - chr.Head.Position).unit
		fireBullet(lookAt)
		wait(0.1)
	end
end

tool.Activated:connect(function()
	if tool.Enabled and not reloading.Value then
		local chr = tool.Parent
		if game.Players:GetPlayerFromCharacter(chr) == script.Parent.plr.Value then
			if chr:FindFirstChild("Humanoid") then
				if chr.Humanoid.Health > 0 then
					reloading.Value = true
					local hum = chr.Humanoid
					local targetPos = hum.TargetPoint
					local lookAt = (targetPos - chr.Head.Position).unit
					local mode = tool.Parent.gunmode.Value
					if mode == 1 then
						--Semiautomatic firing
						semiFire(lookAt)
						wait(0.5)
					elseif mode == 2 then
						--Burst firing
						burstFire(lookAt)
						wait(1)
					else
						--Automatic firing
						autofire = true
						autoFire(hum, chr)
					end
					reloading.Value = false
				end
			end
		end
	end
end)

tool.Deactivated:connect(function()
	autofire = false
end)

3 answers

Try changing your force to this.

force.force = Vector3.new(0,100,0) -- 100 is perfect. its a mostly straight shot

If that doesn't work then put it like this

force.force = Vector3.new(100,0,0)

Or this

force.force = Vector3.new(0,0,100)

The mass of every object in ROBLOX is measured by it's volume. In order to get an object's volume, you have to multiple the Width by the Length by the Height.

local BrickMass = Brick.Size.X * Brick.Size.Y * Brick.Size.Z

This is for any brick.

Now that number alone only gives you mass of the object. Now we have to find out what the exact weight of that object is.

In physics, you learn that the weight of an object is simply M * g, which is the Mass of the object multiplied by the gravitational acceleration in that environment. On Earth, the gravitation acceleration is 9.81 m/s^2. This means that an object in free fall will accelerate at 9.81 m/s^2 downward. The gravitational acceleration in ROBLOX, however, is 196.2 studs/second^2. So to get the weight of the object, take the mass of the object and multiply it by the ROBLOX gravitational acceleration.

local BrickWeight = BrickMass * 196.2

Now that you have the weight of the brick, you need to put that in the force. Like this:

force.force = Vector3.new(0, BrickWeight, 0)

And there you go! That'll keep your bullet in a straight line path all the time!

FULL FUNCTION:

function fireBullet(d)
	local bullet = Instance.new("Part")
	bullet.FormFactor = "Custom"
	bullet.Size = Vector3.new(1.5, 0.35, 0.35)
	bullet.BrickColor = BrickColor.new("White")
	bullet.Position = tool.Handle.Position + (d * 5)
	bullet.Velocity = d * 125

	local s = script.RemoveBullet:Clone()
	s.Disabled = false
	s.Parent = bullet

	local BulletWeight = Bullet.Size.X * Bullet.Size.Y * Bullet.Size.Z * 196.2 --This gets the force required to keep the bullet hovering
	local force = Instance.new("BodyForce")
	force.force = Vector3.new(0, BulletWeight, 0)
	force.Parent = bullet

	bullet.Parent = game.Workspace
end

3 comments

0

uh.... ok?? thanks mr. scientist.

0

Wait, this still doesn't work...

0

Nope.

Look, Do like this:

function fireBullet(d)

    local bullet = Instance.new("Part")

    bullet.FormFactor = "Custom"

    bullet.Size = Vector3.new(1.5, 0.35, 0.35)

    bullet.BrickColor = BrickColor.new("White")

    bullet.CFrame = CFrame.new(script.Parent.Handle.Position,d)

    bulletForce = Instance.new("BodyVelocity", bullet)
    
    bulletForce.maxForce = Vector3.new(math.huge,math.huge,math.huge)

     bulletForce.velocity = bullet.lookVector * 125
 
    game.Debris:AddItem(bullet,3)

    local s = script.RemoveBullet:Clone()

    s.Disabled = false

    s.Parent = bullet

 

    local force = Instance.new("BodyForce")

    force.force = Vector3.new(0, 25, 0)

    force.Parent = bullet


    bullet.Parent = game.Workspace

end

That'd fix the bullet flying in all directions possible error thing. Aight?

This discussion is preserved as it was. New answers, comments, and votes are closed.