Check if player click on another player under a certain distance

Hello !
I’m trying to make an SCP.
So I’m trying to make like when the SCP click on a player under a certain distance '(like 10), it’ll run a script, I got this :

local Player = game.Players.LocalPlayer -- Getting the player	
local Mouse = Player:GetMouse() -- Getting the players mouse]

Mouse.Button1Down:Connect(function() -- Detecting when the player presses the left mouse button
	
	if Mouse.Target.Parent:FindFirstChild("Humanoid") then -- Checking if the Mouse is over a player
		print(Mouse.Target.Parent.Name) -- replace this with whatever code you want 
	end
	
end)

From scriptinghelper, I would just like to know how I can modify it to make it work only under a certain distance

5 Likes

I don’t know about that code, but this sounds like a good use case for proximityprompt, have you checked into that?

2 Likes

I would suggest using UserInputService instead of mouse button. For reasons.
Anyway, to get the distance between two players, all you have to do is some basic maths.
Distance = (Client Position - Otherplayer Position).Magnitude

local UIS = game:GetService("UserInputService")

UIS.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then  -- if input is mouse button 1
	    if Mouse.Target.Parent:FindFirstChild("Humanoid") then  -- If mouse hovering over an object with humanoid in it
	    	local Distance = (Player.Character.HumanoidRootPart.Position - Guard.HumanoidRootPart.Position).Magnitude   -- Calculate distance between the two
            if Distance <= 40 then  -- if distance between the two is less then or equal to 40 studs
                 -- Do something
            end
	    end        
    end
end)
1 Like

You could try something like this

local Player = game.Players.LocalPlayer -- Getting the player
local char = Player.Character or Player.CharacterAdded:Wait()
local Mouse = Player:GetMouse() -- Getting the players mouse]

Mouse.Button1Down:Connect(function() -- Detecting when the player presses the left mouse button
	
	local clickchar = Mouse.Target.Parent
	if clickchar:FindFirstChild("Humanoid") then -- Checking if the Mouse is over a player
		print(Mouse.Target.Parent.Name) -- replace this with whatever code you want 
		local dist = (char.HumanoidRootPart.Position - clickchar.HumanoidRootPart.Position).Magnitude
		if dist <= 10 then
			print("Stuff here")
		end
	end
	
end)

What this should do is get the HumanoidRootPart of both you and whoever you clicked on and get the distance from that via the Magnitude.

Basically as how @amadeupworld2 stated

3 Likes

Will it executes a Server Side script? Or I need to use RemoteEvent?

1 Like

This is clientsided as seen by game.Players.LocalPlayer, so if you want something to happen server sided when the distance is low enough, yes you need a RemoteEvent to be fired

1 Like

In addition to the above, make sure to check server-sided if the player is close enough. This prevents whatever your SCP is from being exploited. (exploiter can still tp to the scp and interact with it though)

1 Like

I think imma add a team statement that’ll check player team before executing script.

1 Like

Maybe I’ll just make a tool, and it’ll fix the possibility for them to pick up tool so… ye
with .Touched event

1 Like

Why would an SCP be teamlocked?

1 Like

Only if you’re on SCP team you can have access to the function, yes

1 Like

Oh, a human-controlled SCP. Alright. You still need to check the distance with the server since the SCP player could just fire loads of remote events to kill everyone.

1 Like