Velocity in the Direction im facing?

Hey i made a Pistol which spawns a bulletpart and now i wanna give the bullet a Velocity*Where im Looking at. I tried newBullet.Velocity =(Vector3.new(100,0,100) * cam.CFrame.LookVector) But for some reason i does not work.

I believe you should be using AssemblyLinearVelocity instead? You can try that & see if anything changes or not

2 Likes

Is there a way whitout AssemblyLinearVelocity?

You could use a BodyVelocity instead to move the bullet maybe, and set the Velocity’s property to what you had before? Idk why you’re not wanting to do it with AssemblyLinearVelocity

I tried to use AssemblyLinearVelocity and it Still does not really work. It was the same whit the normal velocity if i Type newBullet.AssemblyLinearVelocity =(Vector3.new(100,0,100) * cam.CFrame.LookVector) then the Part gets a Velocity but it does not go where im looking at it goes in a different direction(sorry for my bad englisch its not my other tongue)

The only possible issue that I see is that you aren’t defining your camera variable right :thinking:

Is your script a LocalScript or ServerScript?

1 Like

it is a Server script and i defined it like this : local cam = workspace.CurrentCamera

I think it’s going relevant to the server’s camera, if you want it to fire in the direction you’re facing you’d need to use a LocalScript inside StarterPlayerScripts instead since you’re trying to get the actual camera of the player

1 Like

now i did this
local script: tool.Activated:Connect(function() local cam = workspace.CurrentCamera fireEvent:FireServer(,cam) end)
Server Script : `local function fire(player,cam)

newBullet.Velocity = Vector3.new(100,0,100) * cam.CFrame.LookVector	

end
fireEvent.OnServerEvent:Connect(fire)`

But it still doesnt work it just spawn parts .

--Local
tool.Activated:Connect(function()
    local cam = workspace.CurrentCamera
    fireEvent:FireServer(cam)
end)
--Server
local function fire(player, cam)
    newBullet.Velocity = Vector3.new(100, 0, 100) * cam.CFrame.LookVector
end

fireEvent.OnServerEvent:Connect(fire)

Sorry for asking, but did you even manage to define newBullet?

2 Likes

Yea i did i just cutted the Part out for u

Thas the real script Client :`local tool = script.Parent
local player = game:GetService(“Players”).LocalPlayer
local mouse = player:GetMouse()
local fireEvent = tool:WaitForChild(“FireEvent”)

tool.Activated:Connect(function()
local cam = workspace.CurrentCamera
local mousePosition = mouse.Hit.Position
fireEvent:FireServer(mousePosition,cam)
end)`

Server : local RunService = game:GetService(“RunService”)
local Rp = game:GetService(“ReplicatedStorage”)
local ServerScriptService = game:GetService(“ServerScriptService”)
local tool = script.Parent
local fireEvent = tool.FireEvent
local FastCast = require(ServerScriptService.FastCastRedux)
local firePoint = tool.Handle.FirePoint
local ServerStorage = game:GetService(“ServerStorage”)
local bullet = Rp:WaitForChild(“bullet”)
local Turret = script.Parent.Handle
local fireRate = 0.5
local bulletSpeed = 2000
local cam = workspace.CurrentCamera

local function fire(player, mousePosition)
	
	print(player.Name, "fired at", mousePosition,cam)
	--local Direction = firePoint.CFrame.p (mousePosition - firePoint.CFrame.p).unit * 300
	local newBullet = bullet:Clone()
	print("clone")
		newBullet.Position = Turret.Position
		newBullet.Parent = game.Workspace

	newBullet.Velocity =(Vector3.new(1000,0,1000) * cam.CFrame.LookVector) + Vector3.new(0,10,0)
	

	
end
fireEvent.OnServerEvent:Connect(fire)

Are you getting any errors on the Output at all?

1 Like

No errors the only thing is that the Part is always going in the same direction and not the direction i am facing

You don’t need to define your cam variable in the server script, you need to get the argument passed onto the FireServer() function that you called on your LocalScript

It should look something like this:

local RunService = game:GetService("RunService")
local Rp = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local tool = script.Parent
local fireEvent = tool.FireEvent
local FastCast = require(ServerScriptService.FastCastRedux)
local firePoint = tool.Handle.FirePoint
local ServerStorage = game:GetService("ServerStorage")
local bullet = Rp:WaitForChild("bullet")
local Turret = script.Parent.Handle
local fireRate = 0.5
local bulletSpeed = 2000

local function fire(player, mousePosition, cam)
	
	print(player.Name, "fired at", mousePosition,cam)
	--local Direction = firePoint.CFrame.p (mousePosition - firePoint.CFrame.p).unit * 300
	local newBullet = bullet:Clone()
	print("clone")
	newBullet.Position = Turret.Position
	newBullet.Parent = game.Workspace

	newBullet.Velocity =(Vector3.new(1000,0,1000) * cam.CFrame.LookVector) + Vector3.new(0,10,0)
end

fireEvent.OnServerEvent:Connect(fire)
1 Like

Now it gives me this error:Backpack.Handgun.HandServer:23: attempt to index nil with 'CFrame'

Hm, if it returns back as nil could you try this?

local RunService = game:GetService("RunService")
local Rp = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local tool = script.Parent
local fireEvent = tool.FireEvent
local FastCast = require(ServerScriptService.FastCastRedux)
local firePoint = tool.Handle.FirePoint
local ServerStorage = game:GetService("ServerStorage")
local bullet = Rp:WaitForChild("bullet")
local Turret = script.Parent.Handle
local fireRate = 0.5
local bulletSpeed = 2000

local function fire(player, mousePosition, cam)
    print(cam)
    print(cam.CFrame)
    print(Cam.CFrame.LookVector)
	print(player.Name, "fired at", mousePosition,cam)
	--local Direction = firePoint.CFrame.p (mousePosition - firePoint.CFrame.p).unit * 300
	local newBullet = bullet:Clone()
	print("clone")
	newBullet.Position = Turret.Position
	newBullet.Parent = game.Workspace

	newBullet.Velocity =(Vector3.new(1000,0,1000) * cam.CFrame.LookVector) + Vector3.new(0,10,0)
end

fireEvent.OnServerEvent:Connect(fire)
1 Like

Output: Backpack.Handgun.HandServer:16: attempt to index nil with ‘CFrame’

I think I know what I might’ve did wrong, alright

On your LocalScript, can you change that to

local tool = script.Parent
local player = game:GetService("Players").LocalPlayer
local mouse = player:GetMouse()
local fireEvent = tool:WaitForChild("FireEvent")

tool.Activated:Connect(function()
    local cam = workspace.CurrentCamera
    local mousePosition = mouse.Hit.Position
    fireEvent:FireServer(mousePosition,cam.CFrame.LookVector)
end)

And your server script:

local RunService = game:GetService("RunService")
local Rp = game:GetService("ReplicatedStorage")
local ServerScriptService = game:GetService("ServerScriptService")
local tool = script.Parent
local fireEvent = tool.FireEvent
local FastCast = require(ServerScriptService.FastCastRedux)
local firePoint = tool.Handle.FirePoint
local ServerStorage = game:GetService("ServerStorage")
local bullet = Rp:WaitForChild("bullet")
local Turret = script.Parent.Handle
local fireRate = 0.5
local bulletSpeed = 2000

local function fire(player, mousePosition, camDirection)
	
	print(player.Name, "fired at", mousePosition,camDirection)
	--local Direction = firePoint.CFrame.p (mousePosition - firePoint.CFrame.p).unit * 300
	local newBullet = bullet:Clone()
	print("clone")
	newBullet.Position = Turret.Position
	newBullet.Parent = game.Workspace

	newBullet.Velocity =(Vector3.new(1000,0,1000) * camDirection) + Vector3.new(0,10,0)
end

fireEvent.OnServerEvent:Connect(fire)

I believe this will work, I think what I was doing wrong was attempting to find the CurrentCamera's values in a server script

1 Like

It works thanks alotttttttttttt

1 Like