You can move the piece to where the player's mouse is pointing, or where the player's head is pointing.
I will do it with the Mouse position, as it may be easier for your learning.
First, in LocalScript, when you activate RemoteEvent, remember to send LookVector Mouse, so that we can calculate the direction in which the projectile will be launched.
local Player = game.Players.LocalPlayer
local Mouse = Player:GetMouse()
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
Mouse.Button1Down:Connect(function()
local MouseLookVector = Mouse.Hit.LookVector
RemoteEvent:FireServer(MouseLookVector )
end)
Now in ServerScriptService, to capture the Event
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("RemoteEvent")
local Velocity = 100
local ServerStorage = game:GetService("ServerStorage")
RemoteEvent.OnServerEvent:Connect(function(Player, MouseLookVector)
local part = ServerStorage.Lazer:Clone()
local BodyVelocity = Instance.new("BodyVelocity", part)
part.Parent = workspace
part.Position = Player.Character.Torso.Position
part.CanCollide = false
part.Anchored = true
BodyVelocity.Velocity = MouseLookVector * Velocity
end)
I hope I helped, if you have any questions, you can ask me
:)
Note: Your projectile cannot have Anchored = true, if Anchored is activated, BodyVelocity will have no effect on the body.
2 comments
I can suggest that you can use "Raycast" for it, but I'm not pretty sure since I'm not an expert about it.
you can make use of; workspace.CurrentCamera.CFrame.LookVector * 40; 40 is to make the position 40 stud farer, that will be the position player's camera is looking at 40 studs away.