Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a script print out the Highest Value?

Asked by 9 years ago

Firstly, I made a for loop and inside that for loop it creates a IntValue and assigned it with a Parent and a random Value. So after the for loop finishes I want the script to print out the biggest value out of all of the IntValue that has been created from the for loop. But I can't seem to figure out how to do it.

for i = 1,10 do
    wait() print(i)
 local Variable = Instance.new("IntValue", game.Workspace.Bag)--Bag is a empty Model that is inside of Workspace.
    Variable.Name = "Variable "..i
    Variable.Value = math.random(1,20)
if i == 10 then
print(Variable.Value > Variable.Value) --> false
    end
end

1 answer

Log in to vote
0
Answered by
Unclear 1776 Moderation Voter
9 years ago

Solving your problem

Just follow the algorithm you listed in your question.

  1. Make a loop

  2. Inside the loop create an IntValue

  3. Give the IntValue a Parent and a random Value

  4. Finish the loop

  5. Print the biggest value out of all of the IntValues

You screwed up because you did not follow your own rules. Naturally, because of that, you cannot possibly completely expect to get the same result...

So let's follow your rules and finish steps 1 through 4, because it appears that you somewhat did that correctly, albeit in the wrong order.

for index = 1, 10 do -- Step #1
    LoadLibrary("RbxUtility").Create("IntValue")({ -- Step #2
        Parent = workspace.Bag, -- Step #3
        Name = "Variable" .. index,
        Value = math.random(20) -- Step #3
    })
end -- Step #4

Now we're done with 4/5 of it, and we did not screw up at all. Now, let's figure out how to do #5: Print the biggest value out of all of the IntValues.

How would you find the biggest value out of a bunch of numbers? I don't know about you, but this is how I would do it, step-by-step...

  1. Go through each number

  2. For each number, check it for...

2a. If this is the first number, then it is automatically the largest number out of all of the numbers we have checked so far (pause a bit and think about why this is true) OR if there is no largest number so far, then the number is automatically the largest number (this about this as well)

2b. If not #2a, then check if it is larger than the largest number out of all of the numbers we have checked so far. If it is, then the largest number out of all of the numbers we have checked so far including the current one is that number.

  1. Finish going through each number

  2. Print the largest number we found

Pretty simple, right? Let's turn that into code.

-- assuming t is a reference to all of the numbers in a table
local largestNumber -- a reference to the largest number
for index, number in next, t do -- Step #1
    -- Step #2
    if not largestNumber then -- Step #2a, `if index == 1` then works as well
        largestNumber = number
    else -- Step #2b
        if number > largestNumber then
            largestNumber = number
        end
    end
end -- Step #3
print(largestNumber) -- Step #4

Now, we just put the two portions together.

-- Your algorithm
for index = 1, 10 do -- Step #1
    LoadLibrary("RbxUtility").Create("IntValue")({ -- Step #2
        Parent = workspace.Bag, -- Step #3
        Name = "Variable" .. index,
        Value = math.random(20) -- Step #3
    })
end -- Step #4

-- Step #5 of your algorithm
local largestNumber -- a reference to the largest number
for index, IntValue in next, workspace.Bag:GetChildren() do -- Step #1
    -- Step #2
    local number = IntValue.Value
    if not largestNumber then -- Step #2a, `if index == 1` then works as well
        largestNumber = number
    else -- Step #2b
        if number > largestNumber then
            largestNumber = number
        end
    end
end -- Step #3
print(largestNumber) -- Step #4

What to take from all of this

Most people cannot program something because they failed to make a step-by-step process outlining exactly what they want to happen, or because they cannot follow their process at all.

Do not be that sort of person. I am sure you realized by now that all of these steps that I listed were all easy things that you were probably thinking at one point or another but failed to follow. When you think of a plan of attack for writing a program, make sure you remember it and follow it to the letter.

Creating clear instructions and following instructions are arguably the two most important parts of programming; if you cannot make instructions clearly, then how do you expect your computer to possibly do what you want it to?

Ad

Answer this question