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

Getting the UserID of a player by clicking them?

Asked by 5 years ago

So I asked a similar question yesterday. It worked, but not the way I wanted it. So I want the local player to click another player, then get clicked players USER IMAGE I'd think it'd be easy but it's proving to be hard. Any help is appreciated. My idea of the code is;

local player = game.Players.LocalPlayer
local ClkPlr = -- Don't know
-- Then a function to get the userID image using
"https://web.roblox.com/Thumbs/Avatar.ashx?x=250&y=250&Format=Png&userid="..player.UserId
end

1 answer

Log in to vote
0
Answered by
Avigant 2374 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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.

```lua local services = { players = game:GetService("Players"), user_input = game:GetService("UserInputService"), }

local imports = { user_input_utility = require(script.user_input_utility), }

local configuration = {} -- This is the maximum amount of studs away we want to handle clicks. The most we can do is 5,000 studs with my code. The longer we go, the worse the performance is. 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 -- This is input Roblox is handling, such as clicking in the escape menu. We want to ignore this. return end

if input.UserInputType ~= Enum.UserInputType.MouseButton1 then
    -- Ignore the input. This means we won't let mobile or console players use this feature.
    return
end

-- We want to ignore the local player's character when trying to find what they are clicking on.
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

-- We know the local player clicked on a part, but we do not know if the part is an item of the player's character.
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)

-- The image link we want.
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: ```lua 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 ```

0
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. Avigant 2374 — 5y
0
It didn't get deprecated yet. We don't have to reinvent the wheel avigant User#24403 69 — 5y
1
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. Avigant 2374 — 5y
0
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! Notrealac0unt 19 — 5y
Ad

Answer this question