How can I search a table for a specific value?

Hey Developers I have a table which looks like this

local BolderPositions = {
	Bolder = Bolders.Bolder.Position,
	Bolder2 = Bolders.Bolder2.Position,
	Bolder3 = Bolders.Bolder3.Position,
	Bolder4 = Bolders.Bolder4.Position,
	Bolder5 = Bolders.Bolder5.Position,
	Bolder6 = Bolders.Bolder6.Position
}

and then on a Bindable Event that gives the bolder that hit it I wish to find the bolders name and then set it’s position back to where it needs to go.

ServerStorage.Events.LavaHit.Event:Connect(function(Bolder)
	--- Find the bolder with the same Name inside the table then set it's position to that value ---
end)

Use for loops. For example:

local lookingFor = "Bolder"

for i, v in pairs(BolderPositions) do
	-- i = key (e.g. Bolder, Bolder2), v = value (the positions)

	if i == lookingFor then
		-- key found
	end
end
1 Like

If you are looking for Bolder do

BolderPositions["Bolder"]

So like this:

ServerStorage.Events.LavaHit.Event:Connect(function(Bolder)
	BolderPositions[Bolder]
end)

(While the post from @FrozenInferno_Dev works, for readability and simplicity, just use this syntax for dictionaries.)

1 Like

How would I get the value by using it this way?

ServerStorage.Events.LavaHit.Event:Connect(function(Bolder)
	local key = BolderPositions[Bolder.Name]
	Bolder.Position = key.Value
end)

thats what i am trying to do

With a regular table, you could use myTable[Index], with a dictionary you would do myTable[Key]

local BolderPositions = {
	-- Key = Value --
	["Bolder"] = Bolders.Bolder.Position, -- get with BolderPositions["Bolder"]
	Bolder2 = Bolders.Bolder2.Position, -- get with BolderPositions["Bolder2"]
	Bolder3 = Bolders.Bolder3.Position, -- get with BolderPositions["Bolder3"]
	Bolder4 = Bolders.Bolder4.Position, -- etc.
	Bolder5 = Bolders.Bolder5.Position,
	Bolder6 = Bolders.Bolder6.Position
}

Just read the edit, so Bolder.Name would be the key, BolderPositions[Bolder.Name] is the value.

And here are the docs with more detail.

So how can I get the key which is Identical to the Bolder.Name. I’m sorry I’m a rookie when it comes to tables.

A key is a string, if the keys are the same as the name of the instance they are associated with, then Bolder.Name would be the name of the key, because that’s how you named them.

1 Like

I just used a print and I figured it out! So the I’ll give you solution when I get it to work.

Did you get it to work?

char limit

yeah my bad I forgot to give you the solution it worked perfectly I appreciate you checking up on this. I ended up doing this,

local BolderPositions = {
	Bolder = Bolders.Bolder.Position,
	Bolder2 = Bolders.Bolder2.Position,
	Bolder3 = Bolders.Bolder3.Position,
	Bolder4 = Bolders.Bolder4.Position,
	Bolder5 = Bolders.Bolder5.Position,
	Bolder6 = Bolders.Bolder6.Position
}
ServerStorage.Events.LavaHit.Event:Connect(function(Bolder)
	local key = BolderPositions[Bolder.Name]
	Bolder.Position = key
	Bolder.Anchored = true
	Bolder.Transparency = 1
	Bolder.CanCollide = false
	Bolder.CanTouch = false
end)

so by the way, the key is the string associated with the value, the value being the position, and the key being the bolder name

ServerStorage.Events.LavaHit.Event:Connect(function(Bolder)
	local position= BolderPositions[Bolder.Name] -- position is the value, Bolder.Name is the key
	Bolder.Position = position
	Bolder.Anchored = true
	Bolder.Transparency = 1
	Bolder.CanCollide = false
	Bolder.CanTouch = false
end)
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.