Answered by
4 years ago Edited 4 years ago
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.
01 | local Player = game.Players.LocalPlayer |
02 | local Mouse = Player:GetMouse() |
04 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
05 | local RemoteEvent = ReplicatedStorage:WaitForChild( "RemoteEvent" ) |
07 | Mouse.Button 1 Down:Connect( function () |
08 | local MouseLookVector = Mouse.Hit.LookVector |
10 | RemoteEvent:FireServer(MouseLookVector ) |
Now in ServerScriptService, to capture the Event
01 | local ReplicatedStorage = game:GetService( "ReplicatedStorage" ) |
02 | local RemoteEvent = ReplicatedStorage:WaitForChild( "RemoteEvent" ) |
05 | local ServerStorage = game:GetService( "ServerStorage" ) |
07 | RemoteEvent.OnServerEvent:Connect( function (Player, MouseLookVector) |
09 | local part = ServerStorage.Lazer:Clone() |
10 | local BodyVelocity = Instance.new( "BodyVelocity" , part) |
12 | part.Parent = workspace |
13 | part.Position = Player.Character.Torso.Position |
14 | part.CanCollide = false |
17 | BodyVelocity.Velocity = MouseLookVector * Velocity |
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.