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

How do you make a Part face the direction of your mouse in FE?

Asked by 4 years ago
Edited 4 years ago

Hi, first question here!

I'm uncertain how to make a part face the direction of your mouse (continuously) in FE conditions. What I wanted to do was essentially, every time a player right-clicks the screen, a part is created. The part will be placed above the character's head and will constantly face the direction of the mouse.

I understand that I detect right-click through a local script and create a part through a RemoteEvent and a script. I also know how to make a part face a direction with CFrames. What I don't know how to do is how to make it constantly face the mouse.

I looked up different articles and read that the mouse Instance cannot be passed through a RemoteFunction. The only solution I have is using a RemoteFunction to invokeClient and return mouse.Hit in a while loop.

EDIT: I want to emphasize that I do understand that I have to use CFrame.new(part.Position, mouse.Hit.p). I'm asking how do I achieve making all players see the part rotating without the use of a while-loop in a script that invokesClient for a mouse.Hit!

3 answers

Log in to vote
2
Answered by
JesseSong 3916 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

Hello, in order for you to do this you should use a CFrame.

What are CFrames?

CFrames are short for coordinate frames is a type of data that can set the position of and orientation of the positional component basically meaning you can set the position and orientation with a cframe you can also change the rotation of cframe

Here's an example of Cframe:

local cf = CFrame.new(0, 5, 0) * CFrame.Angles(math.rad(45), 0, 0)

All this does is it sets the position (x,y,z) coordinates 5 studs above

So, understanding your question all you have to do is use mouse.p Mouse.p is a function stands for mouse.Position which sets the position of a mouse to something you want here's how you use it.

Part.CFrame = CFrame.new(Part.CFrame.p,mouse.p)

Try experimenting/welding to your preference. If you want all clients to see this change as you stated you should use a remote event RemoteEvent:FireAllClients essentially the server will tell the client that a change is being happening.

Also if you want the part to be made on the mouse position on a request then you should use a UserInputService (UIS).

0
Oh, no, I know how to use CFrames! I just meant how do I make it *constantly* face the mouse so all players will see it rotating. fluffyoreos32 63 — 4y
1
Try using a loop JesseSong 3916 — 4y
1
Or raycasting idk. JesseSong 3916 — 4y
Ad
Log in to vote
0
Answered by 4 years ago

your getting there.. all you need to do is listen for when the mouse moves, then send the mouse position to the server via remote events, and the server will make the part look in that direction

0
Essentially, what you're saying is the while-loop to use a RemoteFunction is unavoidable? fluffyoreos32 63 — 4y
0
well, not exactly, but to add an evemt handler to listen for when the mouse moves, and fires a remote event to update the look vector of the part.. User#23252 26 — 4y
Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

Hello. As JesseSong said, you must use CFrames.

What to use?

You must use the InputChanged RBXScriptSignal of UserInputService and make the part's CFrame the same as the mouse's CFrame.

How do I use it?

You must use it by connecting a function to the RBXScriptSignal. Then, you must have an if statement checking if the input that was changed was the mouse. Finally, set the part's CFrame to the mouse's CFrame by using Player:GetMouse().Hit.

Here's your script:

local part = workspace:WaitForChild("Part")

local mouse = game:GetService("Players").LocalPlayer:GetMouse()

local UserInputService = game:GetService("UserInputService")

UserInputService.InputChanged:Connect(function(input, _)
    if input.UserInputType == Enum.UserInputType.MouseMovement then
        part.CFrame = mouse.Hit
    end
end)

The only thing you could change is the part variable. Set to the part that you want to move with the mouse.

You can also use Instance:WaitForChild() as the part might not be replicated to the client.

Please accept and upvote this answer if it helped.

Answer this question