Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Find the player who just triggered the ClickDetector?

Asked by
6zk8 95
4 years ago
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?

0
Updated my post User#834 0 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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.

0
What about for part.Touched? Would it work the same? 6zk8 95 — 4y
0
*part.Touched 6zk8 95 — 4y
0
yep it work the same Nguyenlegiahung 1091 — 4y
0
@Nguyenlegiahung No it does not, I just wrote the whole thing up top to explain it works differently. User#834 0 — 4y
Ad

Answer this question