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

How to find a player's backpack with a click detector?

Alright, as the title states, I'm having issues finding the backpack of the player who clicked a block. As of now, I have this code:

script.Parent.ClickDetector.MouseClick:connect(function(hit)
	script.Parent:Destroy() -- correctly destroys.
	print(hit)
	game.Lighting.tools.Matches:Clone().Parent = game.Players:GetPlayerFromCharacter(hit).Backpack
end)

As the comment states, the destroy works (as I'd expect), however, it won't clone into the designated location. Whenever I'm in solo mode, I get this in the output:

  • Workspace.Drawer.Matches.Script:4: attempt to index a nil value

If I'm actually in-game, it then displays the error at line 3 instead of 4.

This is NOT a LocalScript. game.Lighting.tools.Matchesdoes exist.

I've read that you can't reference the player through click detection, but I've even bypassed that in the conversion of a tycoon from touch to click detection buttons, letting me know it has to be possible somehow (the method I used for the tycoon didn't function here, sadly).

Thank you for reading, and I hope to find a solution to this issue!

1 answer

✓ Accepted answer

You are able to get the player value from this event. If you look on the wiki, you would get this example.

local CD = script.Parent
local function onMouseClick(Player)
    local Brick = CD.Parent
    if Brick:IsA("BasePart") then
        Brick.BrickColor = Player.TeamColor
    end
end
CD.MouseClick:connect(onMouseClick)
function clicked(player) -- Use the player variable for a path to the player object

2 comments

0

Oh, well, that was much more simple than expected. I forgot about the wiki in this instance, kind of ironic since I usually always look there first. Regardless though, thank you for the solution!

0

Anytime.

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