Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Find the player who just triggered the ClickDetector?

local cd = game.Workspace.Part.ClickDetector

cd.MouseClick:Connect(function()
	print("Yes")
end)

Every tutorial makes getting the player who triggered it way harder than I think it is supposed to be. Can someone show something simple?

1 comment

0

Updated my post

1 answer

✓ Accepted answer

If you look at the documentation the player who clicked the clickdetector is passed along in the event: https://developer.roblox.com/en-us/api-reference/event/ClickDetector/MouseClick

Example:

cd.MouseClick:Connect(function(player)
	-- put your code here
end)

Edit: Touched only passes the other part that touched the part you put the event on. However you can still quickly obtain the player:

part.Touched:Connect(function(hitPart)
	local otherPlayer = game.Players:GetPlayerFromCharacter(hitPart.Parent)
	if otherPlayer then
		-- put your code here
	end
end)

The reason we need the extra if statement is because if the part does not belong to a player, it would fail.

Additionally you have to keep in mind that any accessories/tools the player has will not be picked up using this method. This is because the handle or whatever other part of inside of them it's parent will be the Accessory or Tool instance. Meaning this will fail. However for starters I wouldn't yet worry about this.

4 comments

0

What about for part.Touched? Would it work the same?

0

*part.Touched

0

yep it work the same

0

@Nguyenlegiahung No it does not, I just wrote the whole thing up top to explain it works differently.

This discussion is preserved as it was. New answers, comments, and votes are closed.