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:
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.
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?
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.
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.
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