Placing a item on a players backpack

Hello everyone!
I’m doing a tool system that places a item on your backpack when you click a part,here’s the script!

local itemHandler = script.Parent
local itemsFinder = game.ServerStorage.Items
local Players = game:GetService(“Players”)

function getItem()
if itemsFinder:FindFirstChild(itemHandler.Name) then
print(“This is working!”)

  local player = Players.LocalPlayer
  if player then
  	local clonedItem =  itemsFinder:FindFirstChild(itemHandler.Name):Clone()
  	clonedItem.Parent = player.Backpack
  end

else
print(“No”)
end

end

itemHandler.ClickDetector.MouseClick:Connect(getItem)

2 Likes

Are you having trouble? Do you need help with something?

2 Likes

Yes,the issue is on the post,but i can’t find a way to access a player’s backpack through a server script.

LocalPlayer can not be used inside of server scripts. It will return nil. Instead, for the function, add a parameter called “player” and it should work. Like this:

function getitem(player)
end

clickdetector.MouseClick:Connect(getitem)
4 Likes

I just discovered that,but how can i access a players backpack through a server script?

You can get the player from the click detector and then you put the item in the player’s backpack.

1 Like

I just edited my last reply with something that should work.

1 Like

You know you can get the player that clicked the part right?
Try this

local itemHandler = script.Parent
local itemsFinder = game.ServerStorage.Items
local Players = game:GetService(“Players”)

function getItem(plr)
if itemsFinder:FindFirstChild(itemHandler.Name) then
print(“This is working!”)

  if plr then
  	local clonedItem =  itemsFinder:FindFirstChild(itemHandler.Name):Clone()
  	clonedItem.Parent = plr.Backpack
  end

else
print(“No”)
end

end

itemHandler.ClickDetector.MouseClick:Connect(getItem)
4 Likes

Use
ClickDetector.MouseClick:Connect(function(player)
–type the code to execute
end)

This should let you access the player. If this worked, mark this as a solution.