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
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 ![]()
Is your script a LocalScript or ServerScript?
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
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?
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?
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)
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)
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
It works thanks alotttttttttttt
