Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to make it so a part goes to where a player is looking?

Asked by 3 years ago

So, here is my issue:

I want to make it so a part is being created on the players Torso, then the part will move towards where the player is looking. Thing is: I have no idea how to do it, since I have never touched velocity. Part Creation Script:

local ReplicatedStorage =  game:GetService("ReplicatedStorage")
local RemoteEvent = ReplicatedStorage:WaitForChild("LazerEvent")

RemoteEvent.OnServerEvent:Connect(function(player)
    local part = game.ServerStorage.Lazer:Clone()
    local velocity = Instance.new("BodyVelocity", part)
    part.Parent = workspace
    part.Position = player.Character.Torso.Position
    part.CanCollide = false
    part.Anchored = true

end)

Thanks for helping me.

0
I can suggest that you can use "Raycast" for it, but I'm not pretty sure since I'm not an expert about it. R4nd0ml0l2 105 — 3y
0
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. imKirda 4491 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 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.

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 -- you can change this whenever you want

local ServerStorage = game:GetService("ServerStorage")

RemoteEvent.OnServerEvent:Connect(function(Player, MouseLookVector)
    -- now i will use your code.
    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
    -- To make the projectile go in the right direction

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.

0
Unfortunatly, that didn't work, the part just generates and then stays there. InterDwarf 14 — 3y
0
Did you set Anchored = false? genilsonotavio 132 — 3y
0
oops, I tested the script, and found the error, you can't use game.ServerStorage.Lazer. You must use, game: GetService ("ServerStorage"), And then do ServerStorage.Lazer: Clone () genilsonotavio 132 — 3y
0
local ServerStorage = game:GetService("ServerStorage) genilsonotavio 132 — 3y
View all comments (6 more)
0
Thank you! InterDwarf 14 — 3y
0
Unfortunatly, after testing, that still doesn't work. InterDwarf 14 — 3y
0
Wow, this is really weird, I tested it here and it worked correctly, can you point out the problem? genilsonotavio 132 — 3y
0
But try one last thing. Put the MouseLookVector variable inside the function in LocalScript. genilsonotavio 132 — 3y
0
Also try changing the LazerEvent name to RemoteEvent only, and substituting FindFirstChild() in LocalScript for WaitForChild() genilsonotavio 132 — 3y
0
That still doesn't work... Can you add me in Discord and send me your scripts? whattoputhere#2488 InterDwarf 14 — 3y
Ad

Answer this question