For i, v in pairs Help

Hey programmers,

Im using a module script and im kinda new to these stuff so i need some help

Im trying to make it when you give a petName to the module Function it gives you a number value back

Heres an example:

Pets.MultiplierValue = {
	
	["Bear"] = 1.15;
	["Fox"] = 1.17;
	
	
}
	

Pets.Multiplier = function(petName)
	
	local multiplierValue
	
	for i, v in pairs (Pets.MultiplierValue) do
		
		-- If the PetName is Bear or Fox the multiplierValue will be set to their value from Above
		
	end
	
	return multiplierValue
end

I know this is pretty easy and stuff but i haven’t really ever studied i, v in pairs that much and sometimes linking up everything is confusing to me

Thanks in advance!

1 Like

if I understand correctly I don’t think you would need a for loop at all and you can just do something like this instead:

Pets.Multiplier = function(petName)
  return Pets.MultiplierValue[petName]
end

in the case that what I mentioned above works, you could probably omit the function all together


But if you need to use a for loop then you could do:

for i, v in pairs (Pets.MultiplierValue) do
        if i ==  petName then
          multiplierValue = v
    end 
end
1 Like

It will seem like magic to you, but all you have to do is create an If statement. Honestly, you can try it yourself:

I don’t know about C++ and very, very advanced Lua, but it works. If I knew more about it I would explain it to you in more detail. Hope it’s enough, this is if you want to do this using the for i,v loop.

But I recommend the solution @Jaycbee05 gave you, it just searches the table for what you need and returns it (sounds weird, in other words it’s like you’re making workspace.part, you use the dot-operator. Same thing, only instead of a dot you use another way. You can learn more here, as these are actually dictionaries: Tables | Documentation - Roblox Creator Hub)

P.S.: And if anyone thought so, no, I didn’t copy everything from @Jaycbee05 , there are just limited ways to achieve this result (and i am using a translator and have bad wifi, so this comes late).

2 Likes

Thanks to both of you @Jaycbee05 and @Eternalove_fan32 <3

1 Like