Bag code isn't working

  1. What do you want to achieve?
    I want a bag with items inside to hand to another person with the same contents.

  2. What is the issue?
    When I try to hand the bag with items inside, it always hands it to the other person as an empty bag.

  3. What solutions have you tried so far?
    I have tried many solutions, however they do not work.

Hi,
So basically I have made a system where you can package all the food in your inventory into a bag, then hand it to someone. I have made a hand-to system for this. However, it hands it to the other person as an empty bag.

Here are some photos:
image
Full bag before handing,
image
Empty after.

image
Bag code location.

Here is the client and server code:

Client (the hand-to GUI) -

local player = game.Players.LocalPlayer
local handTo = script.Parent.Parent.HandToText

script.Parent.MouseButton1Click:Connect(function()
	
	local playername = script.Parent.Parent.HandToText.Text:lower()
	local textboxcurrentname = 0
	local playerchosen = nil
	
	if handTo.Text ~= "" then
		for i,v in pairs(game.Players:GetPlayers()) do
			if v.Name:lower() == playername then
				playerchosen = v
				print(playerchosen)
			end
		end
		
		if playerchosen ~= nil then
			print("Player found!")
			
			if player.Character:FindFirstChildWhichIsA("Tool") then
				game.ReplicatedStorage.HandTo:FireServer(playerchosen)
				handTo.Text = ""
				handTo.PlaceholderText = "Handed!"
				wait(1)
				handTo.PlaceholderText = "Hand to"
			else
				handTo.Text = ""
				handTo.PlaceholderText = "Error, no tool equipped!"
				wait(1)
				handTo.PlaceholderText = "Hand to"
			end
		else
			handTo.Text = ""
			handTo.PlaceholderText = "Error, no player found!"
			wait(1)
			handTo.PlaceholderText = "Hand to"
		end
	else
		handTo.Text = ""
		handTo.PlaceholderText = "Error, no text!"
		wait(1)
		handTo.PlaceholderText = "Hand to"
	end
end)

Server (hands the item) -

game.ReplicatedStorage.HandTo.OnServerEvent:Connect(function(plr, plrtogive)
	local toolToGive = plr.Character:FindFirstChildWhichIsA("Tool")
	toolToGive.Parent = game.Players[tostring(plrtogive)].Backpack
end)

The bag client code -

local plr = game.Players.LocalPlayer
local rs = game:GetService("ReplicatedStorage")
local itemValue = game:GetService("ReplicatedStorage").CloneStuff.Items


script.Parent.Equipped:Connect(function(Mouse)
	Mouse.Button1Down:Connect(function()
		if script.Parent.Name == "Empty Bag" then
			local item = false
			for i, v  in pairs(plr.Backpack:GetChildren()) do
				local o = v:FindFirstChildWhichIsA("BoolValue")
				if o and o.Value == true then
					v:Destroy()
					local clone = itemValue:Clone()
					clone.Parent = script.Parent
					clone.Value = v.Name
					item = true
				end
			end

			if item == true then
				script.Parent.Name = "Full Bag"
			end
		elseif script.Parent.Name == "Full Bag" then
			for i, v in pairs(script.Parent:GetChildren()) do
				if v.Name == "Items" then
					if rs.Drinks:FindFirstChild(tostring(v.Value)) then
						rs.Drinks:FindFirstChild(tostring(v.Value)):Clone().Parent = plr.Backpack
					elseif rs.Food:FindFirstChild(tostring(v.Value)) then
						rs.Food:FindFirstChild(tostring(v.Value)):Clone().Parent = plr.Backpack	
					end
					
					v:Destroy()
					
				end
			end
			script.Parent:Destroy()
		end
	
	end)
end)

Any help is appreciated,
Thanks!

1 Like

I don’t know if that’s the problem but that definitely needs to be changed or at least sanity checked. You shouldn’t change the name in the Client or if you do make sure to check the Name In the Server. it appears that the other Client thinks its an empty bag because you change the Name to Full Bag In the Client which won’t apply for the other clients ( correct me if I am wrong )

1 Like

Thanks, I’ll try it out when I have time.

1 Like