How would I go about making a grocery list?

How would I go about making a grocery list? So, I have made a script that makes a folder which is the list and values which are the items the player needs to collect, the value would start at a certain number lets say 3 so the player would need to collect 3 apples so they can complete the list. I don’t know if there is an easier or better of doing this because I am struggling why my code doesn’t work.

If anyone has any suggestions on how to make a better grocery list please say, thanks in advance!

Shopp

Script that subtracts fruit from value

local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(otherPart)
	local char = otherPart.Parent
	local player = Players:GetPlayerFromCharacter(char)
	if player then
		
		local shoppingList = player:FindFirstChild("List")
		local apple = shoppingList:FindFirstChildOfClass("Apple")
		
		if apple then
			if apple.Value > 0 then
				apple.Value -= 1
				script.Parent:Destroy()
			end
		end
	end
end)
6 Likes

there is unfortunately no Apple class

local apple = shoppingList:FindFirstChild("Apple")

when the part is only meant to be touched once, you should immediately disconnect the connection or only connect it once, or else the players might get multiple apples from one apple

6 Likes
local Players = game:GetService("Players")

script.Parent.Touched:Connect(function(hit)
	local char = hit.Parent
	local player = Players:GetPlayerFromCharacter(char)

	if player then
		local shoppingList = player:FindFirstChild("List")
-- check the name of the item you're touching so you don't need to open all the scripts to rename stuff
		local Item = shoppingList:FindFirstChild(script.parent.Name)
		
		if Item then 
			if Item.Value > 0 then
				Item.Value -= 1
				script.Parent:Destroy()
			end
		end
	end
end)
4 Likes

I don’t think this would work because the item is a value in the list, not a part with a script in it.

2 Likes

FYI, don’t put scripts under each grocery item, it’s a bad practice, rather use a component based approach.

2 Likes

Yeah I forgot to copy FindFirstChild correctly but even with that fix it doesn’t work, how would actually make it check for the apple value? I think that is the main problem.

1 Like

Alright I have been planning on using tags for the grocery items.

1 Like

if you have something named “List” in player, and “Apple” inside of it, then it should work

1 Like

Got it to work, I just added another if ____ then statement.

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