How to detect if value is string or int value in dictionary?

How to detect if value is string or int value in dictionary?

for i,v in pairs(savedValues2) do
local Listing = Instance.new(“IntValue”) OR Instance.new(“StringValue”) – want this to change depending on its type
Listing.Name = v[1]
Listing.Value = v[2]
end

Use type()

If they’re IntValue or StringValue objects, then you can use IsA against the instance. If it’s just a primitive type, you can use type or typeof.

for i,v in pairs(savedValues2) do
   if v:IsA("IntValue") then
      -- IntValue
   elseif v:IsA("StringValue") then
      -- StringValue
   end
end

If you’re using primitive values and not ValueBase objects, then you could use type instead:

for i,v in pairs(savedValues2) do
   if type(v) == "number" then
      -- Number
   elseif type(v) == "string" then
      -- String
   end
end
5 Likes