Key value loop not working as intended?

hi folks!
trying to make a randomized loot generation system
im looping through all spawn points on the map, then if the item does spawn (1/3 chance) it goes through with this:

for i, v in pairs(mapItemSpawns:GetChildren()) do
		local rand = math.random(1,3) -- // 33% Chance of item spawn
		
		if rand == 3 then -- // Item Spawn
			local curr = 0
			
			local ran = Random.new()
			local num = ran:NextNumber(0, total)
			print(total)
			
			for key, value in pairs(tempItem) do
				print(tempItem)
				print(tempItem[key], ":", tempItem[value])
				curr += tempItem[value]
				if num > curr then
					continue
				else
					local rarity = curr
					print(rarity)
					break
				end
			end
		end
	end

but in the second loop (key, value), i can only get the “key” and its not even the key, its the value.
currently the dict looks similar to this:

tempItem = {
     ["Common"] = 0.9,
     ["Elite"] = 0.1,
}

so on so forth with different rarities.
when i print the key and value i get:

0.1 : nil

at a complete loss as to why this is happening, so any help is appreciated :slight_smile:

1 Like

Variable “key” is already key of table, while tempItem[key] is actual value of key. Correct version is print(key, tempItem[key])

1 Like

try this:

for key, value in pairs(tempItem) do
    print(tempItem) -- This prints the whole table
    print(key, ":", value) -- This will now print "Common : 0.9"
    curr += value -- This is correct - adding the probability value
    if num > curr then
        continue
    else
        local rarity = key -- Should probably be 'key' not 'curr' if they want the rarity name
        print(rarity)
        break
    end
end

kill me now…
thank you v much for showing me im blind LOL

1 Like

ha lol sometimes you just need an extra boost of help.

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