I am trying to create a shopping game on roblox, with a virtual cart which players can put their selected items into. The script below is meant to stop duplicated items from reaching the cart. What's supposed to happen is that before the item gets transferred into the cart, the script checks the item's ID and then checks the ID of each item inside the cart to see if they match. If they do, then the script will return boolean foundDuplicate as true(thanks climethestair), and that will cause a prompt to show up. For some reason the script, even though line 5 should stop duplicates, still goes on and returns foundDuplicate as false, which causes the item to be duplicated. I already checked if the script is indexing to a value at the right place at line 5, so I know that it should be working. I'm stumped, help?
Note: #Cart:GetChildren()
does work. Because GetChildren()
returns a table of an object's children, Cart:GetChildren
does get identified as a table and #
recognizes it as a table. Please focus less on that.
script.Parent.MouseButton1Click:connect(function() local CartBox = script.Parent.Parent.Parent.Parent.Parent.Cart.CartGUI local Cart = CartBox.CartBody.ItemSelection local ID = script.Parent.Parent.Parent.ID.Value local Itembox = script.Parent.Parent.Parent.Parent.BoxHold.ItemBox local prompt = game.Players.LocalPlayer.PlayerGui.DuplicateItemPrompt local card = script.Parent.Parent.Parent local foundDuplicate = 1 if #Cart:GetChildren() > 2 then -- if there is at least 1 item in the cart for i,item in pairs(Cart:GetChildren()) do -- for every item in the cart, cycles through every item if item:IsA("Frame") and item.Name == "ItemBox" then -- checks if the selected item is an item if item.ImageLabel.ID.Value == ID then -- checks if ID is the same print("Filtered out Info Box... Item is Duplicated") foundDuplicate = 2 -- returns true, goes to foundDuplicate break elseif item.ImageLabel.ID.Value ~= ID then print("Filtered Info Box... Not Duplicated") foundDuplicate = 3 -- returns false, continues cycle end else print("Filtering Non Info Box...") end end end if #Cart:GetChildren() == 2 then -- if there is no items in the cart then card.Parent.Adornee = nil CartBox.Visible = true -- script transfers item to cart local NewBox = Itembox:Clone() NewBox.Parent = Cart NewBox.Visible = true NewBox.ImageLabel.LogoImage.Image = script.Parent.Parent.Parent.ItemImage.Image print("ItemBox Cloned") NewBox.ImageLabel.ID.Value = ID print("Clothing ID transferred to new Item Box") end if foundDuplicate == 2 then -- checks if foundDuplicate is true or not print("foundDuplicate is Executing Prompt") prompt.Enabled = true prompt.PromptBox.Active = true foundDuplicate = 1 elseif foundDuplicate == 3 then wait(0.5) print("foundDuplicate is clean") card.Parent.Adornee = nil CartBox.Visible = true local NewBox = Itembox:Clone() print("Cloned Item Box") NewBox.Parent = Cart NewBox.Visible = true NewBox.ImageLabel.LogoImage.Image = script.Parent.Parent.Parent.ItemImage.Image print("ItemBox Cloned") NewBox.ImageLabel.ID.Value = ID print("Clothing ID transferred to new Item Box") foundDuplicate = 1 end end)
In terms of just cleaning up your code a bit, you have all your declarations in the MouseButton1Click event. That means that you are declaring them all every time you click on the button, you can actually move them outside the event and only declare them once. Since they are outside the event, their scope can still be accessed by the event.
Also, instead of using 1 and 2 to check for duplicates, try using true and false (aka Booleans). They are essentially 1 and 0 values. It also allows you to use cool statements like...
if not foundDuplicate then end -- if foundDuplicate is false if foundDuplicate then end -- if foundDuplicate is true
Cleaned up code here:
local CartBox = script.Parent.Parent.Parent.Parent.Parent.Cart.CartGUI local Cart = CartBox.CartBody.ItemSelection local ID = script.Parent.Parent.Parent.ID.Value local Itembox = script.Parent.Parent.Parent.Parent.BoxHold.ItemBox local prompt = game.Players.LocalPlayer.PlayerGui.DuplicateItemPrompt local card = script.Parent.Parent.Parent local foundDuplicate = true -- Changed this to true (boolean) script.Parent.MouseButton1Click:connect(function() -- Other stuff end)
In terms of your code logic, it looks a bit confusing and overly complicated, but it SHOULD work, I would keep testing your code with print statements to make sure all the values are as they should be. You could even use the Debugger as this will allow you to step through your code 1 line at a time.
blog.roblox.com/2013/11/test-advanced-game-dev-feature-lua-debugger/