How to insert data to a table in this way and count it

Hello everyone. I’m making a taxi job system and I’ve made a function where I need to save data inside a table.
I’m NOT really experienced with tables.

I need to save the stuff in the following order:
JobsAvailableCurrent{
[“JOB1”] = {“SpawnPoint”, “Destiny”, nil, 300},
}

Now, what my function should be doing is counting how many Rows are there, for example since there’s “JOB1”, the new job id will be: “JOB2”.
And I want to store it at the table.

My issue is that it gets replaced, and the JobID is still “JOB1”.

If you have any questions about my problem please ask me.

this is the part of my script:

JobsAvailableCurrent = {
       --How it should be working:
	--["JOB1"] = {"John St.", "Destiny Av.", 300, nil},
	--["JOB2"] = {"Spawn1", "Cool St.", 300, nil}
}

function SpawnNPC(SpawnPoint, JobID, RandDestiny)
	--Spawn NPC Function with those arguments
end

function GenerateCustomerNPC()
	local RandomPickup = SpawnPoints1[math.random(1, #SpawnPoints1)]
	
	if RandomPickup then
		local DIRECTION = RandomPickup.Spawn_Direction.Value
		local JobID = "JOB" .. tostring(#JobsAvailableCurrent + 1)
		local RandomDestiny = Destinies1[math.random(1, #Destinies1)]

		if RandomDestiny then
			local Destiny_Dir = RandomDestiny.Destiny_Display.Value
			
			if Destiny_Dir then
                          --Issue here:
				JobsAvailableCurrent[JobID] = {DIRECTION, Destiny_Dir, nil, 300} --ISSUE HERE.
				print("JOB ID FOR NEW NPC IS : "..JobID)
				print("The table jobs are : "..#JobsAvailableCurrent)
				print("TABLE: ")
				for key, value in pairs(JobsAvailableCurrent) do
					print(key, value[1], value[2], value[3], value[4])
				end 
                        end
               end
         end
end

My output looks like this:
image
As you can see, it’s not storing properly.

I’m NOT experienced with tables or dictionaries that much.
Thanks for your help :slight_smile:

You can try splitting the job ID, then converting the number (currently a string) back to a number, incrementing it, then concatenating it as a new job ID.

1 Like

It seems to be working this way, Thank you so much! :slight_smile:

1 Like

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