Checking what player is clicking

How would I check if a player has clicked a part within a certain radius of the player?

This detects which player clicks.

local ClickDetector = Instance.new("ClickDetector")
ClickDetector.MouseClick:Connect(function(Player)
    --| Code |--
end)

You’d have to use something called magnitude to check the stud distance.

local range = 10 -- range in studs
local partA
local partB

local distanceBetweenParts = (partA.Position - partB.Position).Magnitude

if distanceBetweenParts < range then
-- Do whatever
end
2 Likes

Ahh I see! How would I find out which part the player has clicked? (Without using ClickDetectors)

Put this in a LocalScript in Starter PlayerScripts:

local Players = game:GetService("Players")
Player = Players.LocalPlayer
Mouse = Player:GetMouse()
Mouse.Button1Down:Connect(function()
    print(Mouse.Target)
end)
1 Like