How can I find which part a player has clicked so that I can use the strings inside the part?
Depending on your circumstance, this can vary - you can use a ClickDetector to catch clicks on a given part or model when users aren’t using their tools.
The ClickDetector.MouseClick event then lets you figure out which player clicked the part or model, and works both client-side from a LocalScript, and server-side from a Script.
But how do I get the game to identify that someone has clicked a part and then find that part?
Ah - sorry, I misunderstood your problem here.
For that part, you could possibly either listen to UserInputService.InputBegan, Mouse.Button1Down or other relevant events, and manually perform a raycast to find the target part - this is done from a LocalScript. Without Experimental Mode, you will then need a RemoteEvent to signal the server which part was pressed by the player.
Here’s a demo of the above implemented:
shish_kebab4_ClickAnyPartDemo.rbxl (13.9 KB)
What about Mouse.Target?
If you have the player’s mouse, why would it be better to perform the raycast yourself?
What would the function be to let the script know that the player has clicked a part without knowing what part?
The method is Mouse.Target
You can base it within the studio file As8D provided, but make sure to add at the top
local LocalPlayer = game.Players.LocalPlayer
local Mouse = LocalPlayer:GetMouse()
Replace the 2 lines having to do with the raycast with local Target = Mouse.Target, and that will return the part that the player clicked
I would advise against using the mouse for newer work.
Took a few minutes to do but
function getMouseTarget() local cursorPosition = game:GetService("UserInputService"):GetMouseLocation() local oray = game.workspace.CurrentCamera:ViewportPointToRay(cursorPosition.x, cursorPosition.y, 0) local ray = Ray.new(game.Workspace.CurrentCamera.CFrame.p,(oray.Direction * 1000)) return workspace:FindPartOnRay(ray) end
this successfully uses no Mouse objects and is able to get the part, it also finds the 3d Mouse position ( second return parameter )
local part, mousepos = getMouseTarget() --mousepos is the 3d position of the mouse (Mouse.Hit.p) --part is the part that the mouse is pointing at (Mouse.Target)
Edit: It had already been made @ Wrote up a short script to get 3D mouse hit from UserInputService
could just do
obviously, but if we are using the mouse object then we might aswell use the Mouse.Hit and Mouse.Target
my post was how to get the position and the part that the mouse is poining at without the Mouse Object as its a “legacy API”