Help with my pickup system[fixed myself]

So i’m making a hunger and food pickup system, and it has a bit of a problem. I already made the inventory and the hunger bar they work fine. My problem is the pick up part, i’m using a local script to iterate into a folder with foods scattered around the map here is the script:

UI = game:GetService(“UserInputService”)
OnObject = false

while true do
for i, Foods in pairs(game.Workspace.FoodScattered:GetDescendants()) do
if Foods:IsA(“ClickDetector”) and Foods ~= nil then

function PickUp(Input, Processed)
if OnObject and Input.KeyCode == Enum.KeyCode.E then
if Foods.Parent.Name == “Beans” then
print(Foods)
–print(“beans”)
script.Parent.Main.ToolFoodFolder.Beans:Clone().Parent = game.Players.LocalPlayer.Backpack
game.ReplicatedStorage.RemoveFood:FireServer(Foods)
elseif Foods.Parent.Name == “Burrito” then
–print(“burrito”)
script.Parent.Main.ToolFoodFolder.Burrito:Clone().Parent = game.Players.LocalPlayer.Backpack
game.ReplicatedStorage.RemoveFood:FireServer(Foods)

	end
end

end
UI.InputBegan:Connect(PickUp)

function OnHoverEnter()
Foods.Parent.PromptBillboard.Enabled = true
OnObject = true
end
Foods.MouseHoverEnter:Connect(OnHoverEnter)

function OnHoverLeave()
Foods.Parent.PromptBillboard.Enabled = false
OnObject = false
end
Foods.MouseHoverLeave:Connect(OnHoverLeave)

	end
end

game.Workspace.ChildAdded:wait()
end

The result gives me all the food even though I only interacted with one food, can someone please help? i’m assuming i’m not getting objects properly and should use a table.

1 Like

You cannot clone items into the players backpack from the client.

“script.Parent.Main.ToolFoodFolder.Burrito:Clone().Parent = game.Players.LocalPlayer.Backpack”

You should fire a remote event to clone and place the item within the player’s inventory
But it seems you’re doing this already? Could you explain that line I quoted?

1 Like

Yes that local script is inside a gui and the toolfoodfolder is inside of that gui, more of like an easy access storage, so when it interacts with a burrito it gets a tool in the toolfoodfolder then inserts it in the backpack

Store tools in a place the server can access. The server cant clone a local tool correctly. If the server isn’t cloning the tool and placing the tool inside the player’s backpack, it causes weird replication issues.

1 Like

So storing it somewhere else such as the ReplicatedFirst would make replications work properly?

1 Like

Store it in replicatedstorage, replicated first just makes sure it is replicated right away when a player joins the game. Usually bad practice for stuff like tools.

1 Like

it still doesn’t work, it still gets all the scattered food and gets the ammount and clones into the backpack

I’m confused by what you mean?

So basically ScatteredFood is like all the food in the workspace with clickdetector which are all grouped in a folder, my local script makes a loop so it gets all of them and does the input E then inserts a food tool into the backpack. But my problem is when I try to pick up one, it gets all the food in the scatteredfoodfolder then inserts it all in the backpack instead of just picking up a single one

to simply put it in words

Imagine a kill command

lets say we want to kill someone
you type:
/kill raymond

but instead of killing raymond you kill everyone in the game’s server, mine is similar, it gets all instead of just one

here is the place to better understand it Hunger Script.rbxl (34.3 KB)

Here is the updated place: Hunger Script.rbxl (34.4 KB)

You can see the problem when you test it