How do i find if a key exists under a table value?

I can’t figure this out. It doesnt have to be finding the specific key, it just would need to be able to find a key under this part. Here is the script here:

while wait(0.1) do
	if #list < 25 then
		if math.random(1,10) == 1 then
			local newbug = fly:Clone()
			table.insert(bugslist,newbug)
		end
	end
	if #list ~= 0 then
		for _, v in pairs(bugslist) do
			if not v['destination'] then
				v['destination'] = Vector3.new
			end
		end
	end
end

Poop

2 Likes
for _, v in pairs(bugslist) do

This line means, “iterate over all the key- value pairs in table bugslist”. So this’ll result in
K1=V1
K2=V2

For however many items you have. _ is the key, v is the value. You’re trying to index ‘destination’ on the value, whereas I assume you either want to check if the value is destination, or check if bugslist has a key destination.


--[[
bugslist[key] = value
does key 'destination' exist? Or is there a key, which holds value 'destination?'
Let's check: 
]]
for _, v in pairs(bugslist) do
  if v == 'destination' then
    return v
  end
end



if bugslist['destination'] then
-- This checks if there is a key destination in bugslist, if so continue.
end


--[[You don't need a loop to check for a key, you could look at it as being depth 0.
We can instantly check for a key, but to check for a specific value we need to loop through all keys.
table, 
   | ->  key1,
          | ->   value1,

   | ->   key2,
           | ->   value2,
]]
1 Like

Ok thank you! I simplified this script so it would be an easier flow for you to read. So i still need to use the loop. However I still need to look for a key under the specific value in the table.

1 Like
	if #list ~= 0 then

This may not work, because bugslist is a dictionary (although I’m not sure, it may be fine).

yourTable[yourKey] = yourValue

1 Like

for each value i have under the main list i need its position and future position, do you know if theres another way i could do this because i dont think its going to work.

1 Like

What exactly are you trying to do? Explain the total system in it’s essence, and I’ll see if I can help you out

1 Like

Here I’ll quickly make a flow chart.

while wait(0.1) do
	if #bugslist < 25 then
		if math.random(1,10) == 1 then
			local newbug = fly:Clone()
			newbug.Parent = part
			newbug.Position = Vector3.new(A lot of boring math that works)
			table.insert(bugslist,newbug)
		end
	end
	if #bugslist ~= 0 then
		for _, v in pairs(bugslist) do ---[this is where the problem is]
			if #v == 0 then
				v['destination'] = Vector3.new(Same boring math)
			end
		end
	end
end

Newbug is a clone of fly, which is a BasePart, not a table I assume (because you’re setting it’s position). Then, you’re adding it to bugslist. When you then run the loop, you’re essentially trying to check #BasePart (which doesn’t work; # is for tables). Then, you’re trying to index ‘destination’ to basepart (which again doesn’t work, as it’s for tables). Fly may be a table, but if it’s a part, you’ll need to make it a table. The way I solve this problem (a table which has a part in it) is

local Fly = {
part=Instance.new("Part"),
destination=Vector3.new(0,0,0),
otherProperty=1,
}

This takes you into the territory of table cloning, which relies on you discerning between shallow and deep copies.

In essence, v is newBug, and newBug is a copy of fly. If fly isn’t a table, #v makes no sense, and v[‘destination’] also doesn’t because fly[‘destination’] doesn’t exist as a property of BasePart.

#bugslist does make sense, because bugslist is a numbered list of fly clones. That means we can fetch the amount of items inside of it. _,v then returns _ (the number of the item it’s at, so always an integer), and v (the returned item, can be many different datatypes).

1 Like

I was wondering if creating an index under the part itself would be an issue. I will for sure try running the loop this way, I’ll check back and tell you if it works.

1 Like

I’ve been looking at this when I can, and is this the solution you are talking about?

			local newfly = {
				specpart = fly:Clone(),
				position = Vector3.new((math.random(partpos.X-partsiz.X,partpos.X+partsiz.X))/2,(math.random(partpos.Y-partsiz.Y,partpos.Y+partsiz.Y))/2,(math.random(partpos.Z-partsiz.Z,partpos.Z+partsiz.Z)/2)),
				rotation = Vector3.new(rngrot(),rngrot(),rngrot()),
				destination = nil,
			}
			table.insert(bugslist,newfly)

That could work, yes, but when cloning fly, it won’t be parented to workspace (even if fly is), so you’ll need to set it to workspace. Also, position/rotation won’t translate to specpart by default.

1 Like

Ok so i have no errors and this still will not work.

local runservice = game:GetService('RunService')
local fly = workspace.Fly
local part = script.Parent
local partpos = part.Position
local partsiz = part.Size
local bugslist = {}
local studdist = 4
local function rngrot()
	return math.random(0,360)
end

local function movetopos(part,X,Y,Z,X1,Y1,Z1)
	print('does this work bucko')
	if X==X1 then
		return error
	elseif X<X1 then
		part.Position.X += 0.25
	else
		part.Position.X -= 0.25
	end
	if Y==Y1 then
		return error
	elseif Y<Y1 then
		part.Position.Y += 0.25
	else
		part.Position.Y -= 0.25
	end
	if Z==Z1 then
		return error
	elseif Z<Z1 then
		part.Position.Z += 0.25
	else
		part.Position.Z -= 0.25
	end
end

while wait(5,25) do
	if #bugslist < 25 then
		local newfly = {
			specpart = fly:Clone(),
		}
		table.insert(bugslist,newfly)
		newfly.specpart.Parent = part
		newfly.specpart.Position = Vector3.new((math.random(partpos.X-partsiz.X,partpos.X+partsiz.X))/2,(math.random(partpos.Y-partsiz.Y,partpos.Y+partsiz.Y))/2,(math.random(partpos.Z-partsiz.Z,partpos.Z+partsiz.Z)/2))
		newfly.specpart.Rotation = Vector3.new(rngrot(),rngrot(),rngrot())
	end
	if #bugslist ~= 0 then
		for _, v in pairs(bugslist) do
			local g = v.specpart -- code wouldve been too long and "G" is for geometry instead of "V" as in value
			if v['destination'] == nil then
				v['destination'] = Vector3.new(math.random(g.Position.X-studdist,g.Position.X+studdist),math.random(g.Position.Y-studdist,g.Position.Y+studdist),math.random(g.Position.Z-studdist,g.Position.Z+studdist))
			else
				movetopos(g,g.Position.X,g.Position.Y,g.Position.Z,v['destination'].X,v['destination'].Y,v['destination'].Z)
			end
		end
	end
end

Im so done im sending the whole script at this point

Sorry if I’m too late but, I would use

local RunService = game:GetService(“RunService”)

RunService.HeartBeat:Connect(function()
if table.find(yourTableName, “destination” then
print(“Key found!”)
end
)

Not sure if I did it correct…

Oh ok! thanks i will test this soon

I don’t understand what you’re trying to make, but here’s your code refactored a bit to make it readable.

local runservice = game:GetService('RunService')
local fly = workspace.Fly
local part = script.Parent
local partpos = part.Position
local partsiz = part.Size
local bugslist = {}
local studdist = 4

local function rngrot()
	return math.random(0,360)
end

local function updatePosition(position, target)
	if position == target then
		return error
	elseif position < target then
		return position + 0.25
	else
		return position - 0.25
	end
end

local function movetopos(part, target)
	part.Position = Vector3.new(
		updatePosition(part.Position.X, target.X),
		updatePosition(part.Position.Y, target.Y),
		updatePosition(part.Position.Z, target.Z)
	)
end

local function randomPosition(center, size)
	return Vector3.new(
		math.random(center.X - size.X, center.X + size.X) / 2,
		math.random(center.Y - size.Y, center.Y + size.Y) / 2,
		math.random(center.Z - size.Z, center.Z + size.Z) / 2
	)
end

while wait(5) do
	if #bugslist < 25 then
		local newfly = {
			specpart = fly:Clone(),
		}
		newfly.specpart.Parent = part
		newfly.specpart.Position = randomPosition(partpos, partsiz)
		newfly.specpart.Rotation = Vector3.new(rngrot(),rngrot(),rngrot())
		table.insert(bugslist,newfly)
	end

	for _, bug in pairs(bugslist) do
		local g = bug.specpart
		bug.destination = bug.destination or randomPosition(g.Position, Vector3.new(studdist, studdist, studdist))
		movetopos(g, bug.destination)
	end
end

1 Like

For here I have sufficiently found and tested if I have the destination. For each fly i have in the script, it should be moving to its random destination within a certain area. I have everything ready and it should be working fine, however it has no errors and fails to work. For a clearer read look at the post by @ExercitusMortem, if you could help that would be very helpful.

Here’s an example of something that could potentially work if you’re using the type of table I think you’re using:

if bugslist[--whatever you want] then
     return true
end
1 Like

as shown here this function should be working. Sorry I somehow missed the simplified version had this part missing.

The problem I have right now is that the part will not move with this function:

local function movetopos(part,X,Y,Z,X1,Y1,Z1)
	print('does this work bucko')
	if X==X1 then
		return error
	elseif X<X1 then
		part.Position.X += 0.25
	else
		part.Position.X -= 0.25
	end
	if Y==Y1 then
		return error
	elseif Y<Y1 then
		part.Position.Y += 0.25
	else
		part.Position.Y -= 0.25
	end
	if Z==Z1 then
		return error
	elseif Z<Z1 then
		part.Position.Z += 0.25
	else
		part.Position.Z -= 0.25
	end
end

You can’t change a position like this

-- wrong
Position.X += 0.5

-- right
Position += Vector3.new(0.5,0,0)
1 Like

With the current script i have nowwww. The bug does move around but its destination is staying the same for a few seconds and is stuck in a loop which is weird.

local runservice = game:GetService('RunService')
local fly = workspace.Fly
local part = script.Parent
local partpos = part.Position
local partsiz = part.Size
local bugslist = {}
dv = 5
local function studdist() 
	if math.random(1,2) == 2 then
		return "-"..dv
	else
		return dv
	end
end

local function rngrot()
	return math.random(0,360)
end

local function updatePosition(position, target)
	if position == target then
		return error
	elseif position < target then
		return position + 0.25
	else
		return position - 0.25
	end
end

local function movetopos(part, target)
	part.Position = Vector3.new(
		updatePosition(part.Position.X, target.X),
		updatePosition(part.Position.Y, target.Y),
		updatePosition(part.Position.Z, target.Z)
	)
end

local function randomPosition(center, size)
	return Vector3.new(
		math.random(center.X - size.X, center.X + size.X) / 2,
		math.random(center.Y - size.Y, center.Y + size.Y) / 2,
		math.random(center.Z - size.Z, center.Z + size.Z) / 2
	)
end


while true do
	local counter = 0
	local finalcount = math.random(5,20)
	repeat wait(0.05)
		if counter == 0 and #bugslist < 25 then
			local newfly = {
				specpart = fly:Clone(),
			}
			newfly.specpart.Parent = part
			newfly.specpart.Position = randomPosition(partpos, partsiz)
			newfly.specpart.Rotation = Vector3.new(rngrot(),rngrot(),rngrot())
			table.insert(bugslist,newfly)
		end
		if #bugslist ~= 0 then
			for _, bug in pairs(bugslist) do
				local g = bug.specpart
				if bug.destination == bug.specpart.Position then
					bug.destination = bug.destination or Vector3.new(
						g.Position.X + studdist(),
						g.Position.Y + studdist(),
						g.Position.Z + studdist()
					)
				end
				bug.destination = bug.destination or Vector3.new( -- I suspect something is wrong here
					g.Position.X + studdist(),
					g.Position.Y + studdist(),
					g.Position.Z + studdist()
				)
				movetopos(g, bug.destination)
			end
		end
		counter += 0.05
	until counter == finalcount
end