Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Keep certain variables in a table?

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

2 comments

1

You could do an if statement then nothing else something end

1

Or if not statement then something end

2 answers

✓ Accepted answer

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.

2 comments

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

0

yes, but you can also put the names into a table and loop through the table's names

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.

1 comment

0

Unsure if it's me but this makes it only target the LinkedSword, but i could use this also, thanks!

This discussion is preserved as it was. New answers, comments, and votes are closed.