Trying to add clickdetector to characters, so players can click on each other to then get some achievement info displayed.
I’m having trouble with the click detector not working on your own character.
If I test in editor with 2 players then each player can click the other one but not themselves.
And if I test with just one player then that player can not click himself.
So the script works, but why is this difference? Why can’t the same player click his own character?
It is only 1 script in ServerScriptService:
local function onPlayerClicked()
print("player was clicked")
end
game.Players.PlayerAdded:Connect(function(player)
player.CharacterAdded:Connect(function(character)
if player and character then
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.Parent = character.HumanoidRootPart
ClickDetector.MouseClick:Connect(onPlayerClicked)
ClickDetector.RightMouseClick:Connect(onPlayerClicked)
ClickDetector.MouseClick:Connect(function()
print("blabla")
end)
end
end)
end)
Thanks in advance for helping me figure out what is the issue.
This is an unfortunate consequence of how the legacy mouse object blacklists the local player’s character, you can work around this by attaching an external part to each player’s character (but parenting it elsewhere) and placing the click detector inside of that instead.
local Game = game
local Workspace = workspace
local Players = Game:GetService("Players")
local function OnClickDetectorClicked(_Player, Player) --Clicking player, clicked player.
print(_Player.Name, Player.Name)
end
local function OnPlayerAdded(Player)
local function OnCharacterAdded(Character)
local HumanoidRootPart = Character:FindFirstChild("HumanoidRootPart") or Character:WaitForChild("HumanoidRootPart")
local Part = HumanoidRootPart:Clone()
Part:ClearAllChildren()
Part.CanCollide = false
local Weld = Instance.new("Weld")
Weld.Part0 = Part
Weld.Part1 = HumanoidRootPart
Weld.Parent = Part
local ClickDetector = Instance.new("ClickDetector")
ClickDetector.MouseClick:Connect(function(_Player) OnClickDetectorClicked(_Player, Player) end)
ClickDetector.Parent = Part
Part.Parent = Workspace
end
Player.CharacterAdded:Connect(OnCharacterAdded)
end
Players.PlayerAdded:Connect(OnPlayerAdded)
I’m using this to display a billboard of achievements above the clicked player in a hangout. It makes sense for players to check each other out and also inspect their own stats.