How to make something happen when a player clicks another player?

Well you must make it serverside because the ClickDetector won’t be visible to other players. Put the normal script at ServerScriptService. ( there’s difference between normal script and local script[i put this here because i got confused between of it when i was new])

it is on a server script in ServerScriptService.

There would be a LocalScript in each player that would generate their own ClickDetectors.

However, I would actually recommend not using ClickDetectors as they aren’t exactly pretty.

local players = game:GetService('Players')
local localPlayer = players.LocalPlayer
local mouse = localPlayer:GetMouse()
mouse.Button1Down:Connect(function()
	mouse.Button1Up:Wait()
	local target = mouse.Target
	if target then
		local character
		local player = players:FindFirstChild(target.Parent.Name)
		if not player then
			player = players:FindFirstChild(target.Parent.Parent.Name)
		end
		if player then
			print(player)
			--code
		end
	end
end)

This instead uses the player’s mouse to detect when the local player clicks on another player’s character. This detection is even better as it will detect the click no matter which part you click, even an accessory.

This appears to be working :slight_smile:
However, last thing, how do I make my GUI appear when they click the player?

In the part that says --code, you can write code to add a GUI. Idk if you have one already made or what, but you’d basically do something like:

(Assuming that ‘gui’ is defined and is to be cloned from somewhere such as ReplicatedStorage)

gui:Clone().Parent = localPlayer.PlayerGui
1 Like

You’re trying to find UpperTorso in the player instance, but UpperTorso is a part of the character model.
Try:

local players = game:GetService("Players")
local clickadd = Instance.new("ClickDetector")

players.PlayerAdded:Connect(function(player)
   local character = player.CharacterAdded:Wait() --// Wait before the character loads
   clickadd.parent = character:FindFirstChild("HumanoidRoodPart")
end)

clickadd.MouseClick:Connect(function(plr)
--// Your code here
end)

The code above will only work for one player, if you want to do it for multiple players, do this:

local players = game:GetService("Players")

players.PlayerAdded:Connect(function(player)
   local clickadd = Instance.new("ClickDetector")
   local character = player.CharacterAdded:Wait() --// Wait before the character loads
   clickadd.parent = character:FindFirstChild("HumanoidRoodPart")

   clickadd.MouseClick:Connect(function(plr)
        --// Your code here
   end)
end)

You should also check if the player didn’t click himself, like this: if plr ~- player then

Hope this works.

Actually, Roblox automatically blacklists the Player’s character from mouse detection.

1 Like