Help with Getting Values from Table

Here’s the problem:

NPCNAME = npcModule[NPC_Value.Value] -- this gets the NPCName in the module script

for i, v in pairs() do --WHAT DO I PUT IN THE PARENTHESIS?!
	local randnumber = tonumber(v)
end

However I lack the proper knowledge to code that, I’m trying to get the highest value so I don’t have to set a SET VALUE, plus so it has more freedom in getting that value in other NPCNames

local table = {
  ["NPCName"] = {
    [1] = { <<----- THIS IS WHAT I NEED TO GET!
    ...dialog stuff
    },
    [2] = { <<----- THIS IS WHAT I NEED TO GET!
    ...dialog stuff
    },
    [3] = { <<----- THIS IS WHAT I NEED TO GET!
    ...dialog stuff
    },
  }
}
1 Like

If I understand correctly, the variable NPCNAME now corresponds to your module’s table['NPCNAME']?
In that case,

for i, v in pairs(NPCNAME) do

will loop through the key-value pairs of NPCNAME, so the values will be:

i = 1, v = {...} --first thing you need to get
i = 2, v = {...} --second thing you need to get
i = 3, v = {...} --third thing you need to get

Since your keys are all integers, consider using for i,v in ipairs instead of pairs, which guarantees your keys will be iterated through in the proper numerical order. pairs would also include any non-integer/non-numerical keys but would not guarantee a specific order.

I’m trying to get the highest value so I don’t have to set a SET VALUE, plus so it has more freedom in getting that value in other NPCNames

I didn’t understand what you meant by that so the solution I’m offering above might not be what you’re actually looking for.

I’ll try it out sometime and see if it works, thank you!

EDIT: For clarification, its for an npc thing I’m working on, the only way they can pick between different sets of dialog is from a math.random, which is how I ‘randomize’ between dialogs. I want to change this for the reasons above :)))

Got an error! Surprisingly…
image

Here’s the rest of the randomizing code -

for i, v in ipairs(NPCNAME) do
	local randnumber = tonumber(v) --It stems from here \/ \/ \/
	if randnumber > highestnumber then --The error is here
		highestnumber = randnumber
	end
end

Here’s where I got this code, my fault if I somehow used it incorrectly. More of a builder than a programmer:

What are you trying to do with tonumber? What does v look like?

Is the npcModule the same as the table, or is the table stored somewhere else?


If npcModule is the same as the table, then this should work:

local function getRandomDialogForNPC(npcName)
	local t = npcModule[npcName]

	return t[math.random(#t)]
end

print(getRandomDialogForNPC(NPC_Value.Value))

If they aren’t the same, then this should work unless the table is stored outside of the script or is named differently:

local function getRandomDialogForNPC(npcName)
	local t = table[npcName]

	return t[math.random(#t)]
end

print(getRandomDialogForNPC(npcModule[NPC_Value.Value]))

I was trying to get the integers, which are [1], [2], [3], ← these ones
Those are also the v’s if you can call them that
Does that not work with tonumber? Like I said, not the best programmer, so I don’t know much of these terms :sweat_smile:

You cannot tonumber a table

That makes much more sense, I’ll try the other guy’s suggestion :saluting_face:

What were you trying to achieve by doing that if it did work?

I don’t really know how to rephrase this, so I’ll try to visualize it the best I can.

local npcModule = {
   ["Jeremy"] = { --this is the NPC Name
      [1] = { --this is a dialog path
           "this is some dialog idrk"
      },
      [2] = { --this is a dialog path
           "this is some dialog idrk"
      },
      [3] = { --this is a dialog path
           "this is some dialog idrk"
      },
      [4] = { --this is a dialog path
           "this is some dialog idrk"
      }
   }
}

return npcModule

Up above is what my module script generally looks like
And down below is my local script for the dialog picker

--This is merely a visualization, not the actual code
for i, NPCs in pairs(npcFolder:GetChildren()) do
   NPCs.HumanoidRootPart.ProximityPrompt.Triggered:Connect(function()
      NPC_Value.Value = NPCs.Name
      NPCNAME = npcModule[NPC_Value.Value]
      
      --then some random code that doesn't need to be typed out

      Random_Dialog = math.random(1, 9)

      task.wait(0.35)
      cameraTweenIn:Play()
      startDialog(Random_Dialog, NPCNAME)
   end)
end

That math.random(1, 9) is what I want to fix. The thing is, I want numerous NPCs in my game, with their own amount of dialog paths. If Jeremy has 9 sets of dialog paths and another has 7, how would it get the specific path from the other if the math.random goes above the NPC’s total number of dialog paths? It’s also tiring to pump out natural and unique dialog for all the NPCs in my game just to match that.

So, if I pick the highest number of dialog paths from that specific NPC, it’s less of a hassle for me. No need to make more just to accommodate for the other NPC/s.

this is what I have right now:

for i, v in ipairs(NPCNAME) do
	local randnumber = #v
	if randnumber > highestnumber then
		highestnumber = randnumber
		print(highestnumber)
	end
end

The thing is, it prints out the number of dialogs in the dialog path, not the amount of dialog paths itself, which is really weird to me.

switch to this #NPCNAME. This will get the max number of keys within your dictionary. These keys are your dialogue paths

1 Like