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
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 ```