Multiplying values from a table of strings

To keep this short, I’m attempting to multiply randomly selected IntValues which are stored in ReplicatedStorage, using the corresponding randomly selected tags from this table.

local tags = {Two, Five, Four}  -- Examples
for i, tag in pairs(partTags) do
	
	odds.Text = "1/ " .. repStorage.Attributes:FindFirstChild(tag).Value -- I want to have them multiplied here.

end

The solution to this particular problem would have the text equalling 1/40
I’ve been trying to get a solution but I’m clueless, any push in the right direction is appreciated :>

2 Likes

I read your text about 4 times and I couldn’t understand it. Could you explain in more detail what the problem is?

I would like to multiply the table contents together, but the actual numerical values are stored in replicated storage, i then want to add the final result of the multiplication to the Text, which is inside a gui, see code block.

-- avoid unnecessary abbreviation of names, e.g. use ReplicatedStorage instead of repStorage

-- please avoid using the name Property or Attribute for folders
-- because those have special meaning in roblox, you may use Stats instead

-- also avoid using 'tag' for variable, it also mean a thing in roblox
-- in your use case you use FindFirstChild, so it better be using 'statName'

local statNames = { "Stat1", "Stat2", "Stat3" }

local product = 1
for _, statName in statNames do
	local intValue = ReplicatedStorage.Stats:FindFirstChild(statName)
	if intValue and intValue:IsA("IntValue") then
		product *= intValue.Value
	end
end
odds.Text = `1/{product}`
1 Like

I’m unsure why you’re commenting on the way I named some variables but could you please explain the code you wrote instead? I’d like to learn something instead of taking something I don’t know how to use.

The script only returns the number 1, had to adjust the variables you replaced and it doesn’t really help me much.

i assume you had your structure like this: (using my suggested namings though)

ReplicatedStorage
  |__ Stats
        |__ Stat1 : IntValue = 2
        |__ Stat2 : IntValue = 5
        |__ Stat3 : IntValue = 4

and you seem want to multiply all the values from Stat1, Stat2, Stat3
which means you want product = 2 * 5 * 4 = 40

local product = 1
for _, statName in statNames do
	local intValue = ReplicatedStorage.Stats:FindFirstChild(statName)
	if intValue and intValue:IsA("IntValue") then
		product *= intValue.Value
	end
end

so this part of the code does that.

because we want to multiply, we first want the product to be 1
in the loop, we find the IntValue by its name, make sure it is found and of type IntValue,
then we multiply its Value with product.
so in the first iteration we get product = 1 * 2
the second iteration we get product = (1 * 2) * 5
and finally we get product = (1 * 2 * 5) * 4

at last, we put the result into the text

Not sure if this could help, but you can get a random out of a table using:

local Table = -- your table
local Random = Table[math.random(1, #Table)] --> Random out of table.
1 Like

These Values are not concrete, I have around 20 random IntValues stored in ReplicatedStorage.

The local script this code is inside waits for a mouse to hover over specific parts in the workspace which have different. randomly chosen tags assigned. These tags are then displayed on a gui - That part is done.
Meanwhile, I wanted to display the rarity of each part. Each Tag shares a name with an IntValue in ReplicatedStorage, and I wanted to multiply those together for the final number, also displayed on the gui. - This is where I’m stuck

This sadly isn’t relevant as the randomness aspect of my code is kinda sorted by this point, sorry.

1 Like

there is no ‘randomness’ in your original code so i don’t know how you want the ‘randomness’ to be

local tags = {Two, Five, Four} -- Examples
no randomness here
and you said you wanted the result to be 1/40 . which is ALL of these “tag values” multiplied together
2 * 5 * 4 = 40

No, no i didn’t…

Also this post wasn’t directed towards you?
If you don’t have a solution, you’re not inclined to respond, I’ll just have to figure something out a different way.

It was as I had asked. I believe that everyone here could give you an answer, but there was a lack of information. For someone who needs help, you need to be as explicit as possible, not just in a tiny part of your code with little information.
Another point is that many players use the forum as a kind of “ticket&support” for other users to answer questions that already have answers in other posts.
Try searching the forum for other posts or in the documentation and even on YouTube for something about your question.

I posted the necessary block and explained everything, theres not much use posting anything else would do. I spend hours scouring videos and the forum for similar posts too so this was my last resort.