How do I get Backpack from ServerScript

Hi. I’m trying to make a Shop Gui and the problem is that when you use a Local Script to get Tools it just bugs the Tool so I’m currently trying to use Server Script.

I made this Script but it still won’t work somehow:

local Players = game:GetService("Players")
local Player = Players:GetPlayers()
local Backpack = Player.Backpack

local Tool = game.ReplicatedStorage.TestFolder.Test

script.Parent.MouseButton1Click:Connect(function()
	Tool:Clone().Parent = Backpack
	print("Test")
end)

I found a related post of this but it’s broken for me and probably outdated.

1 Like

There’s a trick I use to fetch the player from a server Script inside of a Gui:

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local guiButton = script.Parent

local player = guiButton:FindFirstAncestorWhichIsA("Player")

local tool = ReplicatedStorage.TestFolder.Test

if player then
	local function onMouseButton1Click()
		tool:Clone().Parent = player.Backpack
		print("Test")
	end

	guiButton.MouseButton1Click:Connect(onMouseButton1Click)
end

Since Guis will be placed inside of the player’s PlayerGui you can use FindFirstAncestorWhichIsA to fetch the player

2 Likes

you can use this local script:

local player = game.Players.LocalPlayer
local backpack = player:WaitForChild(“Backpack”)

I’m pretty sure that objects inside replicated storage can’t be cloned or removed. Put the backpack tools into server storage instead

They can be cloned/removed? They’re supposed to be REPLICATED for client and server use.

ServerStorage is when something has to be replicated by the server

I think you should try this updated script instead. Make sure to write the correct name of the GUI and the button in the Button variable.

local Tool = game.ReplicatedStorage.TestFolder.Test
game.Players.PlayerAdded:Connect(function(plr)
	task.wait(2)
	local Button = plr.PlayerGui.Gui.Button-- The name of the Gui and the button
	Gui.MouseButton1Click:Connect(function()
		Tool:Clone().Parent = plr
		print("Test")
	end)
end)

Create a RemoteEvent that Fire Server with Tool Name (if its same name as the button then do this) when you click on a button.
Client:

script.Parent.MouseButton1Click:Connect(function()
	Event:FireServer(script.Parent.Name)
end)

Then you get the event from server and then handle the giving tool:

Event.OnServerEvent:Connect(function(player, tool)
	local backpack = player.Backpack
	local tool = -- look for the tool or where you put it
		
	tool:Clone().Parent = backpack
end)

Yeah saw this and it worked fine.

1 Like

Already did that but shows an error previously

It actually can using Server Script

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.