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

Why is my script destroying all children instead of just the ones with certain names?

Asked by 6 years ago

I want this script to go through the player backpack and destroy certain items, but leave the others. Right now, it iterates through the backpack, checks the child's name, prints the name... and then destroys it, regardless of whether that name is in the list of children that I want destroyed. What am I missing?

local backpack = player.Backpack
    local children = backpack:GetChildren()
    for i = 1, # children do
        local child = children[i]
        if child.Name == "Chainsaw" or "ChainsawCheck" or "Claws" or "ClawsCheck" or        "Container2Check" or "Container3Check"
                        or "Container4Check" or "Container5Check" or "Container6Check" or "Container7Check" or "Container8Check"
                        or "Container9Check" or "DozerCheck" or "FireAxe" or "FireAxeCheck" or "Flamethrower" or "FlamethrowerCheck"
                        or "Grenade" or "GrenadeCheck" or "Halberd" or "HalberdCheck" or "Hammer" or "HammerCheck" or "Jackhammer"
                        or "JackhammerCheck" or "KatCheck" or "Lucy" or "LucyCheck" or "Machete" or "MacheteCheck" or "RollerCheck"
                        or "Saw" or "SawCheck" or "SledgeHammer" or "SledgeHammerCheck" or "SuperSaw" or "SuperSawCheck" 
        then
            print(child.Name)
            child:Destroy()
        else
        end
    end
0
What's the point of check? MRbraveDragon 374 — 6y
0
This is just a small bit of the script, all of the checks such as "ChainsawCheck" are there to keep track of whether the player has ever purchased the item from the store before so that the store knows they just need a fresh copy of the tool instead of needing to pay for it again. Buggims 4 — 6y

2 answers

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

You're missing child.Name == for all the other checks

If false or true always results in true.

if "AppleJuice" then
    print("Hello oops?")
end

I added some in but I'll let you fill in the rest.

local backpack = player.Backpack
local children = backpack:GetChildren()
for i = 1, # children do
    local child = children[i]
    if child.Name == "Chainsaw" or child.Name == "ChainsawCheck" or child.Name == "Claws" or "ClawsCheck" or        "Container2Check" or "Container3Check"
                    or "Container4Check" or "Container5Check" or "Container6Check" or "Container7Check" or "Container8Check"
                    or "Container9Check" or "DozerCheck" or "FireAxe" or "FireAxeCheck" or "Flamethrower" or "FlamethrowerCheck"
                    or "Grenade" or "GrenadeCheck" or "Halberd" or "HalberdCheck" or "Hammer" or "HammerCheck" or "Jackhammer"
                    or "JackhammerCheck" or "KatCheck" or "Lucy" or "LucyCheck" or "Machete" or "MacheteCheck" or "RollerCheck"
                    or "Saw" or "SawCheck" or "SledgeHammer" or "SledgeHammerCheck" or "SuperSaw" or "SuperSawCheck" 
    then
        print(child.Name)
        child:Destroy()
    else
    end
end

You should make a table so you don't have to type so many ors and it'll be easier to edit later on.

local checksTable = {"Check1", "Check2", "And so on"}

0
Thank you for showing me exactly what I did wrong! I understand it now and won't make the same mistake again (hopefully!) I did switch it over to a table too, much easier to edit and read. Buggims 4 — 6y
Ad
Log in to vote
0
Answered by 6 years ago
local Names = {"Chainsaw", "ChainsawCheck", "Claws", "ClawsCheck", "Container2Check"}
-- Add as many names to the table above as you please

local backpack = player.Backpack
local children = backpack:GetChildren()

for i = 1, #children do
    local child = children[i]
    for a,name in pairs(Names) do
        if child.Name == name then
            print(child.Name, name)
            child:Destroy()
            break
        end
    end
end
0
Don't just give the script... Explain why you are using a table. And what is wrong with the script MRbraveDragon 374 — 6y
0
Thanks! This did solve the issue, but if anyone wouldn't mind taking a moment to explain why my own attempt did not work, I'd appreciate it, I under how and why this solution works, but I don't fully understand why my own did not. Buggims 4 — 6y

Answer this question