#How do I find the players backpack?

85 messages · Page 1 of 1 (latest)

rose hare
#

Hey I wanna make it so that when I click a specific part it adds a tool to the players backpack, however I don't know how to tell the game to put it in the players backpack since I am getting an error. (The following code is part of a script inside of a tool)
The error: Workspace.Tool shop.Dumbell2.Script:7: attempt to index nil with 'Backpack'

My code:

local Player = game.Players.LocalPlayer
local PlayerHasTool = false

ClickDetector.MouseClick:Connect(function(playerWhoClicked: Player) 
   if PlayerHasTool == false then
       game.ServerStorage.Dumbell2:Clone().Parent = Player.Backpack
       PlayerHasTool = true
       
   else
       return
   end

   
end)
   
coral wadi
west lynx
#

Utilize the playerWhoClicked parameter

#

Instead of using a debounce to permanently disable your ClickDetector.MouseClick listener, simply use "Once" instead of "Connect"

#

Know that only one player in the entire server's lifetime is able to pick up that dumbbell

rose hare
#

but im still having the issue with the backpack

graceful vigil
#

Are you sure you're not having issue with the dumbbell clone?

graceful vigil
#

Actually, is this in a local script?

rose hare
#

no

graceful vigil
#

Then you can't use LocalPlayer.

rose hare
#

oh

#

that makes sense

graceful vigil
#

Just make it a local script.

rose hare
#

I made it a local script

#

but theres probably an issue with the clone thing

#

because its not giving me the item

graceful vigil
#

Wait, let me fix the script for you.

#

local ClickDetector = script.Parent.ClickDetector

ClickDetector.MouseClick:Connect(function(Player)
    game.ServerStorage.Dumbell2:Clone().Parent = Player.Backpack
    ClickDetector:Destroy()
end)

#

If you want to ClickDetector to be destroyed for one person only, put it in a local script, if you want the ClickDetector to be destroyed for everyone then put this in a server script.

graceful vigil
rose hare
#

no

graceful vigil
#

Try Player:WaitForChild("Backpack")

rose hare
#

still doesnt work

#

with no error message

#

also when I add this print statement it doesnt print

graceful vigil
#

Yeah, I was about to ask you to do that.

#

Are you sure that you're clicking the clickdetector properly?

rose hare
#

my cursor turns into the click thing

#

and then I click

graceful vigil
#

Show me a picture of the workspace.

rose hare
#

heres the click detector

#

this is the rest

graceful vigil
#

Move the Dumbell2 to ReplicatedStorage instead.

west lynx
rose hare
#

I should also change it in the script right?

graceful vigil
#

ServerStorage is not visible to the client.

graceful vigil
rose hare
#

still no

graceful vigil
#

Just listen to what that other guy said at the beginning.
He seems to know a lot more than I do.

rose hare
west lynx
# rose hare

This looks fine to me. Just make sure you use a Script

rose hare
#

ok

#

it works

#

I just had to change it to a script

#

thanks guys

graceful vigil
graceful vigil
#

I know what a client and a server is.

west lynx
#

Not to a sufficient degree

#

Read the articles

graceful vigil
west lynx
#

It's crazy how you're not reading the articles

#

The answer to that question is openly present in one of them, lol

graceful vigil
#

Alright, I'll ready 3 entire articles for that one answer.

#

Actually, nvm.

#

I thought you told me to read all of this.

#

I'll see then.

west lynx
#

You should read the rest anyway

graceful vigil
#

Well, I haven't been coding for long but I usually just try to learn along the way.
Althought it maybe isn't the most optimized way, it's the most fun way.

Like, I used for loops to move parts "smoothly" until like yesterday when I found out about tweenservice.

west lynx
#

See this list?

graceful vigil
#

I just read it.

west lynx
#

Those are the only locations where LocalScripts can execute, disregarding some exceptions

#

Your LocalScript isn't going to run as a descendant of Workspace

#

Even if it did, you'd now have to deal with the replication boundary

west lynx
#

A Script can freely execute under Workspace, and it is also executed under the server's authority. You need the pick-up action to replicate

graceful vigil
#

So if I have a local script parented in a tool then it won't work?

#

It's a mircale I don't have a single local script in the thing I'm currently making.

#

Well, now I know. 👍

west lynx