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

How would I get a player from a clickdetector?

Let's say I have a click detector like this:

script.Parent.ClickDetector.MouseClick:connect(function()
local player = --this Is what I want to know how to get
local f = script.Parent
f.Parent = player.Backpack
end)

How do I get the player? I know with a brick touch you can:

function onTouch(f)
local player = game.Players:GetPlayerFromCharacter(f.Parent)
end
script.Parent.Touched:connect(onTouch)

and you can get the player like that through touch, but in the situation I'm in I don't want to use a brick touch. Is there a similar way for click? If you can help please do! Thanks!

2 answers

✓ Accepted answer

ClickDetector's event MouseClick is already emitted with a argument of who the player is that clicked the Part.

Fixed Code


script.Parent.ClickDetector.MouseClick:connect(function(playerclicked) -- Here's the argument
local f = script.Parent
f.Parent = playerclicked.Backpack
end)

3 comments

2

To be noted: ClickDetectors will not work in FilteringEnabled. As an alternative, use a LocalScript that will detect what a person clicked on with the mouse's hit property. With that LocalScript you can then fire to the server what you want it to do.

0

Oh :P, totally forgot about it. Thanks for remembering.

0

Thanks so much!

You can simply use an argument with it... Something like his

script.Parent.ClickeDetector.MouseClick:connect(function(player)
if player then
local f = script.Parent
f.Parent = player.Backpack
end
end)

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