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

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

Asked by
RoboFrog 400 Moderation Voter
9 years ago

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

Log in to vote
2
Answered by
bbissell 346 Moderation Voter
9 years ago

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
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! RoboFrog 400 — 9y
0
Anytime. bbissell 346 — 9y
Ad

Answer this question