Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Why does my script ignore my if then statement comparing values?

Asked by 5 years ago

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)


0
why don't you use foundDuplicate as a boolean (true or false) rather than integers? chomboghai 2044 — 5y
1
Why have you posted a duplicate question, and then tried to hide it by deleting the old one? https://i.imgur.com/IgmGfQ7.png fredfishy 833 — 5y
0
Instead of deleting old questions and making new, just edit the original question. No foul play is being done here, fredfishy. minikitkat 687 — 5y
View all comments (21 more)
0
Why would you delete the old question unless you were specifically trying to cover up the fact that you knew you shouldn't be reposting the same question? fredfishy 833 — 5y
0
Why would he try to hide it if it's the same question? minikitkat 687 — 5y
0
He didn't put in his full script, so it seemed that instead of editing it, he made a new one to correct the mistake. minikitkat 687 — 5y
0
He had edited in the full script, the cache was slightly out of date. He received a couple of answers as well referring specifically to line 11. I obviously can't prove this because the cache is all I have. fredfishy 833 — 5y
0
odd, what kind of foul play do you think he would be up to then? What possibly could he gain from doing this? Just seems like he's new to the site to me. minikitkat 687 — 5y
0
Bumps the question up to the tippy top. From what I remember it was about to fall off the first page. fredfishy 833 — 5y
1
Mo benefit if he's already gotten answers. He knows not to do it now though, so best to just let it go. minikitkat 687 — 5y
0
Im going to start off with saying im sorry, for not knowing the rules on how to use scripting helpers fully. I thought this was simply a site to answer questions dedicated to scripting issues on roblox. I didn't know the full intricacy of this site and that is my own fault. But 2 quams i have. 1, what is the matter with people on this site? I understand I broke the rules, BronzedMocha 60 — 5y
0
I don't understand the heavy amount of policing in this site. What's the goal? BronzedMocha 60 — 5y
0
This leads to 2, what is the matter with this site in general? There's no general order to the site, why is there no categories? Why is there a group of moderators for a site so disordered? Why are the moderators so loyal to this site despite it being so disordered? BronzedMocha 60 — 5y
0
Why is there reputation? Why is there a disgust towards beginning developers from those with high reps? Why do those moderators have a mall cop complex as if they are providing justice by shutting down questions BronzedMocha 60 — 5y
0
Reposting a question is one of the only things that make sense on this site. You want answers, and since every question is funneled into 1 section and upvoting is only giving to higher reps, of COURSE an asker would duplicate their question to get back onto the top. But instead of natural human logic, you fred decide to paint a picture as if I was partaking in illegal activity. BronzedMocha 60 — 5y
0
It's not disordered, bad questions and bad users make it disordered. Besides I'm sure you would aprecieate it more if we had the least amount of control over you. The site is vastly community driven, but we still have a small and dedicated staff team. There isn't heavy policing, but staff will take necessary action if anyone is breaking the rules and not using the site as intended. minikitkat 687 — 5y
0
Reputation provides credibility, and perks and abilities to help keep the site in order. The primary reason you see veterans look down on newbies, is because newbies largely do not read the rules, or know how to use the site effectively or as it is intended. minikitkat 687 — 5y
0
nobody but those devoted to the site have understanding on the rules, because it is simply presented as a forum for roblox devs at surface. And it is disordered, because obviously it is meant for more advanced developers, not for those who are beginners. If you guys had a way to divide questions between those who are experienced and those who aren't, then this site would make sense. BronzedMocha 60 — 5y
0
The site is friendly for beginners and experienced. I don't see how you get the idea that it is merely presented as a forum. You can learn pretty much anything about the site here: https://scriptinghelpers.org/help minikitkat 687 — 5y
0
If you guys had a way to divide questions based on their category, it would also make sense, but you don't. You dont present your rules to anyone who is casually asking a question. Most of the active moderators are as unhelpful and judgmental as your buddy fred, and reputation goes to negative numbers. Reputation is good in concept, but answers are altered because of the level of reputation. BronzedMocha 60 — 5y
0
No, they're not. We don't have categories because your title should tell us everything about it. Reputation doesn't change your question at all. If you can ask and answer efficiently and constructively you will do just fine. I am doing my best to be as helpful as possible, and I hope you know I am not doing this to be a jerk. minikitkat 687 — 5y
0
I know you're not a jerk. You're one of the better people on this site. But you cannot speak for all moderators when you say reputation does change your question. And even though the titles do give a bit of insight to the question, all types of questions get funneled into one spot. When it falls off the first page it wont be answered. nobody upvotes unless the question is interesting to them. BronzedMocha 60 — 5y
0
We are continuously working to provide more incentive to up vote questions, and unfortunately nowadays, not many people up vote or answer questions. I can assure you that all of our mods have good intentions, and if you believe you've been treated unfairly, you can file a complaint at https://scriptinghelpers.org/contact but before you do so, make sure that you are familiar with the rules. minikitkat 687 — 5y
0
It can just be so much better with less condescending mods, questions that don't fall off after an hour without being answered, a replacement to reputation, or at least taking out neg reps, thats all. BronzedMocha 60 — 5y

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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/

0
I used foundDuplicate as a num value instead of a boolean cause i wanted to check if the script was actually searching for children in the cart, and not dodging it and firing foundDuplicate from when the value was first identified. And yeah my scripts are not so clean, but i'm trying to get better at it. Yeah i'm just going to figure out another way to stop duplicates. BronzedMocha 60 — 5y
Ad

Answer this question