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
4 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

01script.Parent.Touched:Connect(function(hit)
02    local char = hit.Parent
03    local hum = char:FindFirstChild("Humanoid")
04 
05    if hum then
06        local players = game:GetService("Players")
07        local player = players:GetPlayerFromCharacter(char)
08 
09 
10        local pack = player.Backpack
11        local findSword = pack:GetChildren()
12        for i,v in pairs(findSword)do
13            if v:IsA("Tool") then do
14 
15                    if v.Name == ("LinkedSword") then end -- part im struggeling with
View all 42 lines...

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 — 4y
1
Or if not statement then *something* end Spjureeedd 385 — 4y

2 answers

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

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

1for _, tool in ipairs(findSword) do
2    if not tool or tool.Name == "LinkedSword" then
3        continue
4    end
5 
6    tool:Destroy()
7end

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 — 4y
0
yes, but you can also put the names into a table and loop through the table's names R_alatch 394 — 4y
Ad
Log in to vote
0
Answered by 4 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.

01for i,v in pairs(findSword)do
02            if v:IsA("Tool") then do
03                    if v then                           
04                        if v.Name ~= ("LinkedSword") then
05                             v:Destroy()
06                       end
07                    end
08                end
09            end
10        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 — 4y

Answer this question