Need experienced scripters for a problem!

So, I am making a gun and want to create a bullet that launches in the direction of gun. I have two problems:

  1. The Bullet does not move.
  2. How do I make the bullet move in the direction its facing?

Here is the code for the BodyVelocity:


local event = script.Parent:WaitForChild("Shoot")
local debounce = false
event.OnServerEvent:Connect(function(player, target, gun)
	if not debounce then
		debounce = true
		
		local bullet = game.ReplicatedStorage.Tools.Bullet:Clone()
		bullet.CFrame = gun.Handle.CFrame
		
		local bodyVel = Instance.new("BodyVelocity")
		bodyVel.MaxForce = Vector3.new(1000000,1000000,1000000)
		bodyVel.Velocity = Vector3.new(0, 10, 0)
		bodyVel.P = 2000
	
		bullet.Parent = game.Workspace
		bodyVel.Parent = bullet
		
		gun:FindFirstChild("Sound"):Play()
		local victim = target.Parent
		local victimPlayer = game.Players:GetPlayerFromCharacter(victim)
		
		if victim then
			local character = victim
			local humanoid = character:FindFirstChild("Humanoid")
			--[[if target.Name == "Head" then
				humanoid:TakeDamage(50)
			else
				humanoid:TakeDamage(20)
			end]]
		end
		wait(0.2)
		debounce = false
	end
end)

Here is a video of the problem:

robloxapp-20210505-1917453.wmv (950.7 KB)

1 Like

You can use CFrame.LookVector for which way the bullet is facing.

1 Like

CFrames are the way to go.

CFrame.lookVector returns your direction that you want to go into.

You can choose any number for speed.

To get a Velocity you need a Scaler and a Direction.

So…

local direction = Bullet.CFrame.lookVector
local speed = 5

local Velocity = speed * direction

Bullet.BodyVelocity.Velocity = Velocity
1 Like

Thanks! That looks promising. I will try it out.

Also of the bullet does not move there would be a few reasons why.

Check that it isn’t anchored.
Check that there are no other forces or constraints negating the body Velocity
Check that there is only one Body Velocity
Check that the Maxforce is set high enough. Consider using ‘inf’ math.huge or some arbitrary large number just in case.

I think this will work for your BodyVelocity:

local bodyVel = Instance.new("BodyVelocity")
bodyVel.MaxForce = Vector3.new(1000000,1000000,1000000)
bodyVel.Velocity = (gun.Handle.CFrame.LookVector * 100)  -- * speed
bodyVel.P = 2000

bodyVel.Parent = bullet    --put this bodyVel in the bullet

Let me know if this works. :+1: :smiley:

1 Like