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

How would I get a player from a clickdetector?

Asked by 8 years ago

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

Log in to vote
1
Answered by
rexbit 707 Moderation Voter
8 years ago

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)

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. M39a9am3R 3210 — 8y
0
Oh :P, totally forgot about it. Thanks for remembering. rexbit 707 — 8y
0
Thanks so much! jimborimbo 94 — 8y
Ad
Log in to vote
2
Answered by 8 years ago

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)

Answer this question