✓ Accepted answer
First, let's get start by getting the part a player is clicking on, and checking if they are a player.
LocalScript in game.StarterPlayer.StarterPlayerScripts.
local services = {
players = game:GetService("Players"),
user_input = game:GetService("UserInputService"),
}
local imports = {
user_input_utility = require(script.user_input_utility),
}
local configuration = {}
configuration.maximum_click_distance = 500
configuration.image_link_format = "https://web.roblox.com/Thumbs/Avatar.ashx?x=250&y=250&Format=Png&userId=%d"
local player = services.players.LocalPlayer
local function on_user_input_began(input, is_engine_handled_input)
if is_engine_handled_input == true then
return
end
if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
return
end
local mouse_target_ignore_list = {
player.Character,
}
local mouse_target = imports.user_input_utility.get_mouse_target(input, configuration.maximum_click_distance, mouse_target_ignore_list)
if mouse_target == nil then
return
end
local mouse_target_player = services.players:GetPlayerFromCharacter(mouse_target.Parent)
if mouse_target_player == nil then
return
end
local clicked_player_image_link = string.format(configuration.image_link_format, mouse_target_player.UserId)
print(clicked_player_image_link)
end
services.user_input.InputBegan:Connect(on_user_input_began)
Here's the user_input_utility ModuleScript inside the above LocalScript:
local user_input_utility = {}
local services = {
workspace = game:GetService("Workspace"),
}
local camera = services.workspace.Camera
function user_input_utility.get_mouse_target(input, maximum_distance, ignore_list)
if ignore_list == nil then
ignore_list = {}
end
local unscaled_ray = camera:ScreenPointToRay(input.Position.X, input.Position.Y)
local scaled_direction = unscaled_ray.Direction * maximum_distance
local scaled_ray = Ray.new(unscaled_ray.Origin, scaled_direction)
return services.workspace:FindPartOnRayWithIgnoreList(scaled_ray, ignore_list)
end
return user_input_utility
4 comments
Note that I am deliberately avoiding using the PlayerMouse object, and the PlayerMouse.Target property. This instance is legacy, and I imagine it will be deprecated within the next few years.
It didn't get deprecated yet. We don't have to reinvent the wheel avigant
No, it has not been deprecated yet. However, it is a legacy and useless instance which serves no purpose, and I don't think well-written code should be using it any longer.
Thank you so much. This was very helpful, read through the whole thing to figure out how to get it to work the way i want it to!