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

Conditiional sentences always bugging and ruining my game can you help me figure out whats wrong?

Asked by
TNTIsLyfe 152
4 years ago
Edited 4 years ago

The problem is that conditional sentences they keep bugging for me i tried elseif and even else if (seperate) but the problem is it always registers the first "if" even if the requirements are not met and its really annoying here is a example of what i mean

local No = math.Random(1,20)
if No = 4 or 8 or 12 or 16 then
print("Multiple of 4")
elseif No = 5 or 10 or 15 or 20 then
print("Multiple of 5")
else
print("Random Number")
end

The problem is that it will always print Multiple of 4 even if the requirements arent met and it really annoys me alot i even tried

local No = math.Random(1,20)
if No = 4 or 8 or 12 or 16 then
print("Multiple of 4")
else
if  No = 5 or 10 or 15 or 20 then
print("Multiple of 5")
else
print("Random Number")
end
end

But it still does the same result of always printing the if statement even if requirements arent met so can u help me figure out whats the problem?

0
My apologies for the complicated response, please read my comments. Ziffixture 6913 — 4y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
4 years ago
Edited 4 years ago

You’re using the assignment operator in a conditional statement, which essentially confuses the logical operation, it’s understanding "assign he variable No to 4". There is a large difference between the assignment and comparison operands within coding languages, and this needs to be understood quickly as you can cause a lot of semantic errors, as seen above.

The operator you need to use is ==. This doesn’t solve your problem however, you cannot recursively call or in this scenario without properly providing another comparison expression, this is what’s causing the conditional to pass regardless of the malformed statement.

On a positive note, you can concur if a number is a multiple of a factor by using the modulo operator %, which retrieves the remainder of a division; in the case of x being a factor of y would be 0. This will allow us to run a single condition, without needing to compensate for range.

local Number = Random.new(tick()):NextInteger(1,20)

local Factor1 = 4
local Factor2 = 5

if ((Number%Factor1) == 0) then
    print(Number.." Is a factor of "..Factor1)
elseif ((Number%Factor2) == 0) then
    print(Number.." Is a factor of "..Factor2)
else
    print(Number.." isn’t a factor of "..Factor1.." or "..Factor2)
end

Note:

if calls a conditional scope. elseif provides an alternative condition. else concludes all conditions aren’t met.


If you’re wondering what Random.new() is, this is a more dirverse form of math.random. It returns a Random Object that can be initiated with a seed, which allows us to build a more randomized pseudo-random number. It contains many methods allowing us to be versatile on our return calculation as well. TL;DR: More efficient and versatile.

0
wut?? TNTIsLyfe 152 — 4y
0
can u please explain in simple terms? u are like wut... TNTIsLyfe 152 — 4y
0
meow my eyes are burning TNTIsLyfe 152 — 4y
0
My apologies I overused technical jargon. This isn’t easy to level down as a boolean logic is quite a heavy topic for beginners. Essentially, I said that your if-statements are poorly formatted, the code cannot recognize you’re trying to compare two values to each other. Ziffixture 6913 — 4y
View all comments (5 more)
0
You’re also going to end up having to compare too many numbers to verify to see if a number is a multiple of 4 or 5, and that’s only for a range between 1-10. There is a mathematical operator called modulo which can get the remainder of a division as said above. We can easily determine if a number is within the family of 4 or 5 by devising our number by it; 15 / 5 is 3 leaving no remainders (0) Ziffixture 6913 — 4y
0
For the :NextInteger() line, this will prevent your code from picking the same number from 1-20. Computers cannot ever randomly pick a number, a special "seed" is used to make it seem like it’s picking a number when it’s really just a complex equation. Since the equation is static, it will always come back to the same answer with the same numbers it was fed, by giving it a new seed Ziffixture 6913 — 4y
0
We can effectively make a bias to the equation, which will give it a more “random” selection. I use tick() to give it the current time as you could say; this time changes every second, meaning after a while the number it uses in the equation will be different. Ziffixture 6913 — 4y
0
I’ve tested my code, it works absolutely fine. Ziffixture 6913 — 4y
0
oh thanks TNTIsLyfe 152 — 4y
Ad
Log in to vote
-1
Answered by
iuclds 720 Moderation Voter
4 years ago
local multiples = {[4] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20}, [5] = {5, 10, 15, 20}}
local No = math.Random(1,20)

for i,v in pairs(multiples) do
    for a,b in pairs(v) do
        if No == b then 
        print('Multiple of',i)
        return
    end
end
0
There is absolutely no need to downvote my answer. Ziffixture 6913 — 4y

Answer this question