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

Keep certain variables in a table?

Asked by
pie9909 18
3 years ago

Okay, so I figured out how to used tables to delete tools from char , and backpack, but what are the ways I can keep a certain variable in a table?

here's my code so far

script.Parent.Touched:Connect(function(hit)
    local char = hit.Parent
    local hum = char:FindFirstChild("Humanoid")

    if hum then
        local players = game:GetService("Players")
        local player = players:GetPlayerFromCharacter(char)


        local pack = player.Backpack
        local findSword = pack:GetChildren()
        for i,v in pairs(findSword)do
            if v:IsA("Tool") then do

                    if v.Name == ("LinkedSword") then end -- part im struggeling with
                    if v then                            
                        v:Destroy()


                    end
                end 
            end
        end


        local Char = player.Character or player.CharacterAdded:Wait()
        local equippedSword = Char:GetChildren()
        for i,v2 in pairs(equippedSword) do
            if v2:IsA("Tool") then do



                    if v2 then
                        v2:Destroy()


                    end
                end
            end
        end
    end
end)        


If there are other forms that can explain this to me further, I'd love the read them

1
You could do an if statement then *nothing* else *something* end Spjureeedd 385 — 3y
1
Or if not statement then *something* end Spjureeedd 385 — 3y

2 answers

Log in to vote
0
Answered by
R_alatch 394 Moderation Voter
3 years ago
Edited 3 years ago

Try using continue to skip to the next iteration of the for loop.

for _, tool in ipairs(findSword) do
    if not tool or tool.Name == "LinkedSword" then
        continue
    end

    tool:Destroy()
end

All I did was make your if statements into a single if statement that checks if any of the conditions are false. If one is, it will continue to the next iteration of the for loop.

0
Thanks! and if I wanted to ignore another tool, I can just add another tool name on line 2 right? for example if not tool or tool.name == "linkedSword" or tool.name == "otherTool" then etc... pie9909 18 — 3y
0
yes, but you can also put the names into a table and loop through the table's names R_alatch 394 — 3y
Ad
Log in to vote
0
Answered by 3 years ago

So from my understanding you are trying to ignore one of the tools. Here is the for loop that I rewrote to serve your needs.

for i,v in pairs(findSword)do
            if v:IsA("Tool") then do
                    if v then                            
                        if v.Name ~= ("LinkedSword") then
                             v:Destroy()
                       end
                    end
                end 
            end
        end

This simply checks to see if the tool isn't named "LinkedSword" In case you are wondering ~= checks if things are not equal.

0
Unsure if it's me but this makes it only target the LinkedSword, but i could use this also, thanks! pie9909 18 — 3y

Answer this question