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

How would I make an if statement check for something inside a table?

I'm trying to make an if statement check for stuff in the backpack of the player and thought of using tables but I'm not sure how I'll do it I've been looking on the dev forum and stuff for a while now but can't find anything helpful if anyone could help me I would be very happy if you want me to explain more what I'm trying to do just ask

3 comments

0

i mean, you still have to try. you dont need to find answers, you need to come up with it yourself. if something doesnt work, this forum is for that, not for fullout scripts.

0

I'm not looking for a fullout script I'm asking how I would go about doing something like that it's hard to come up with something when you don't know where to start

0

if you don't know where do start, you probably don't know much about lua. the first thing i thought about was for loops, but you didn't think of anything, which is saying something.

2 answers

There is an easy way to do this actually. Your first option is to make the backpack a table

local tbl = plr.Backpack:GetChildren()
if tbl[something] then
print('works')
end

Your other option is just if something exists in there

if plr.Backpack:FindFirstChild('something') then
print('works')
end

1 comment

0

I know I could do that but the problem is there's going to be alot in the player backpack so I would have to make a super long line doing that what I want is an if statement to check for multiple parts and if it finds one of those parts it triggers

Finding specific things in Array-like tables

local myArray = {"a", "b", "c", "d"}

if table.find(myArray, "b") then
	print("b Exists!")
end

Finding specific things in Dictionaries

local function findDictionary(t, v)
	for k, tv in pairs(t) do
		if tv == v then
			return k
		end
	end
end

if findFictionary({a="a", b="b"}, "b") then
	print("b Exists!")
end

JavaScript like find function

local function tableFind(t, f)
	for k, v in pairs(t) do
		if f(k, v) then
			return v
		end
	end
end

if tableFind({{Name = "Alice"}, {Name = "Bob"}}, function(k, t) t.Name == "Bob" end) then
	print("Found bob")
end

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