For i,v in pairs() not working!

I’m making a game where you need to build a spaceship, and obviously there is a building mechanic and it works perfect.
Now I have a module and this script inside of the driver seat, what i want the server script to do is search through the engine part of the table on the module script and determine if there is a engine located on the ship, although instead of going through the whole table all it does is stop at the first type of engine and doesn’t go through the rest in the table.

Module:

local repStorage = game:GetService("ReplicatedStorage")
local buildingFolder = repStorage:WaitForChild("Build")
local buildingItems = buildingFolder.BuildingItems
local SeatFolder = buildingItems:WaitForChild("Seats")
local EngineFolder = buildingItems:WaitForChild("Engines")
local OtherFolder = buildingItems:WaitForChild("Other")

local items = {
	["engines"]  = {
		["basic_engine"] = {
			price = 50,
			power = 70,
			part = EngineFolder:FindFirstChild("BasicEngine"),
			name = "BasicEngine"
		},
		["advanced_engine"] = {
			price = 100,
			power = 120,
			part = EngineFolder:FindFirstChild("AdvancedEngine"),
			name = "BasicEngine"
		},
		["jet_engine"] = {
			price = 350,
			power = 180,
			part = EngineFolder:FindFirstChild("JetEngine"),
			name = "BasicEngine"
		},
		["rocket_engine"] = {
			price = 800,
			power = 250,
			part = EngineFolder:FindFirstChild("RocketEngine"),
			name = "BasicEngine"
		}
	},
	
	["seats"] = {
		["basic_seat"] = {
			price = 15,
			part = SeatFolder:FindFirstChild("BasicSeat"),
			name = "BasicSeat"
		},

		["basic_driver_seat"] = {
			price = 30,
			part = SeatFolder:FindFirstChild("BasicDriverSeat"),
			name = "BasicDriverSeat"
		}
	},
	
	["other"] = {
		
	}
}

return items

Code that isn’t working inside of the ServerScriot inside a VehicleSeat:

script.Parent.Changed:Connect(function()
	if script.Parent.Occupant then
		script.LinearVelocity.Enabled = true
		for _, engine in pairs(engines) do
			print(engine.name)
			local a = script.Parent.Parent:FindFirstChild(engine.name)
			if a then
				local engine = script.Parent.Parent:FindFirstChild(engine.name)
				local seat = script.Parent
				local front_speed = engine.power
				local up_speed = 1
				local max_height = 300
				print("YEAH!")
				while true do
					wait(.5)

					script.AngularVelocity.AngularVelocity = Vector3.new(0, -1 * seat.Steer, 0)
					if seat.Throttle == 1 then
						script.LinearVelocity.VectorVelocity = Vector3.new(0,up_speed,-front_speed)
					end

					if seat.Throttle == -1 then
						script.LinearVelocity.VectorVelocity = Vector3.new(0,-up_speed,front_speed)
					end

					if seat.Throttle == 0 then
						script.LinearVelocity.VectorVelocity = Vector3.new(0,0,0)
					end

					--if math.random(seat.Position.Y - seat.Position.Y / 2) == 0 then
					--	script.LinearVelocity.VectorVelocity = Vector3.new(0,0,0)
					--end

					--if seat.Position.Y + seat.Size.Y / 2 > max_height then
					--	script.LinearVelocity.VectorVelocity = Vector3.new(0,-up_speed,-front_speed)
					--end
				end
				return
			end
		end
	else
		script.LinearVelocity.Enabled = false
	end
end)

and a screenshot of the explorer:
image

for one, it looks like you have a while true do that never breaks, for second it looks like you return after finding one engine part which will terminate the connection regardless of where you are. i would recommend
1: use task.spawn(function() end) on the while true do loop to run it on a separate thread (maybe use a promise as well to clean it up idk) OR create a base case to break the loop and allow the for loop to continue
2: try not returning at the end of detecting the first engine type instance

if a then
				local engine = script.Parent.Parent:FindFirstChild(engine.name)
				local seat = script.Parent
				local front_speed = engine.power
				local up_speed = 1
				local max_height = 300
				print("YEAH!")
				while true do
					wait(.5)

					script.AngularVelocity.AngularVelocity = Vector3.new(0, -1 * seat.Steer, 0)
					if seat.Throttle == 1 then
						script.LinearVelocity.VectorVelocity = Vector3.new(0,up_speed,-front_speed)
					end

					if seat.Throttle == -1 then
						script.LinearVelocity.VectorVelocity = Vector3.new(0,-up_speed,front_speed)
					end

					if seat.Throttle == 0 then
						script.LinearVelocity.VectorVelocity = Vector3.new(0,0,0)
					end

					--if math.random(seat.Position.Y - seat.Position.Y / 2) == 0 then
					--	script.LinearVelocity.VectorVelocity = Vector3.new(0,0,0)
					--end

					--if seat.Position.Y + seat.Size.Y / 2 > max_height then
					--	script.LinearVelocity.VectorVelocity = Vector3.new(0,-up_speed,-front_speed)
					--end
                    -- [DANE] are there any base cases that you have to break the while true do loop after finding an engine?
				end
				return -- [DANE] you are terminating the connection here after finding ONE engine, not sure if that's what your goal is
			end

The return at the end is for if “a” is found inside of the part, it doesn’t find it as if it did it would print YEAH!. same with the while true do, it’s just for if the part is found.

Just tested it with the loop and the return toggled as a comment and it does the same thing.

It look like you are looking for a name within a table that matches an Instance name.

Here is the important part of your code, redone so it can find the name:

local items = {
	["engines"]  = {
		["basic_engine"] = {
			price = 50,
			power = 70,
			name = "BasicEngine"
		},
		["advanced_engine"] = {
			price = 100,
			power = 120,
			name = "BasicEngine"
		},
		["jet_engine"] = {
			price = 350,
			power = 180,
			name = "BasicEngine"
		},
		["rocket_engine"] = {
			price = 800,
			power = 250,
			name = "BasicEngine"
		}
	},

}

for index, value in items.engines do
	
	local a = script.Parent.Parent:FindFirstChild(engine.name)
	
	if index == a then
		print("Found Jet Engine") -- or whatever engine
	end
end

i,v in pairs works for other scripts (using the same module) so that wouldn’t be the problem.

i was dumb and forgot to change name to the actual name

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