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

I’m trying to put a clickDetector inside of the player. When another player clicks on them, I want it to open a Gui.

I’ve tried to figure it out myself, but I ran in to a variety of issues.
Thanks! :slight_smile:

Let me know if you need any more information.

2 Likes

Can we see the part of the script containing the errors, or all of it?

1 Like

Every script I’ve tried so far has not worked. However, none of them produced any errors. I finally gave up and came here.

Here’s one of the scripts I tried:

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

players.PlayerAdded:Connect(function(player)
	clickadd.parent = player.UpperTorso
end)

clickadd.MouseClick:Connect(function(plr)

end)
2 Likes

What type of script is this, and what code are you executing in the MouseClick event?

1 Like

This is a normal script in ServerScriptService.

I plan on executing something like this:
game.ReplicatedStorage.Race:Clone().Parent = plr

1 Like

You should clone the ClickDetector before parenting or instance a new one each time. Currently, your ClickDetector is going into one player, and then being moved to a different player each time a player joins. Also, you’re putting it into the Player instead of the Player’s Character.

Would you mind providing a code example?

You can’t place any object in the player’s character rigs. Instead place it on the player’s HumanoidRootPart.
(You can actually place a object into player’s rig. But this must be a custom character)

Current script:

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

players.PlayerAdded:Connect(function(player)
	clickadd:Clone().parent = player.HumanoidRootPart
end)

clickadd.MouseClick:Connect(function(plr)

end)

Reference to @MightyDantheman
Make it

players.PlayerAdded:Connect(function(player)
local clickadd = Instance.new("ClickDetector")
clickadd.Parent = player.Character.HumanoidRootPart
--- puting just player will just return nil as theres no children/ properties on the player do put .Character
local function ClickedPlayer(user)
-- the user is for the person who clicked it.
user:WaitForChild("PlayerGui"):WaitForChild("YourGuiHere", 45).Enabled = true
end
clickadd.MouseClick:Connect(ClickedPlayer)
end)

Instead cause it will just create 1 instance when the server was started

local players = game:GetService("Players")
players.PlayerAdded:Connect(function(player)
    local clickadd = Instance.new("ClickDetector")
	clickadd:Clone().parent = player.HumanoidRootPart
end)

clickadd.MouseClick:Connect(function(plr)
--bleh
end)

This should work.

Also does this need to be server-side, or can it be client-side?

If it does need to be server-side, it should be:

local Players = game:GetService('Players')
Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		local click = Instance.new('ClickDetector',character:WaitForChild('HumanoidRootPart'))
		click.MouseClick:Connect(function(plr)
			print(plr)
		end)
	end)
end)

It doesn’t need to be server sided. It just needs to work.
also, this didn’t work. There’s nothing in the output for some reason.

It didn’t work. Here’s the error:

HumanoidRootPart is not a valid member of Player "Players.CbrahDev"  -  Server - Script:5

It is server sided script
here

It did not work because that is not the character. That is just the array of the player. Try adding .Character and also i updated the script

Use mine

I know, I was just asking if they needed it to be server-side as this seems more like a client-side situation.

1 Like

Once again, I can do client-side, or serverside. :slight_smile:

I’m talking about the code I wrote.