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

How to corectlly put 2 conditions?

Asked by
Gigaset39 111
1 year ago

Hi, in my game usually the same ”round” happens 2 times in a row (Ex: let say that the first round was ”Classic mode”, i dont want that the 2 round to be again ”Classic mode”,

soo i created a par called randomizer that changes colour every 60 seconds, whit this i think that is gona be ”harder” to choose 2 of the same rounds.. soo in the ”MainScript” i sayd:

local randomAll = workspace.randomizer.BrickColor
local nr = math.random(1,7)

   if nr == 1 and randomAll.BrickColor.new == ("Really red") then
           .........
          end

is just that, my lua scripting dictionary is kinda empty,.. so i dont get the coorect formula, do you can help me? i tried another combinations too,but i dont get it right..

hence, my reasoning is that, the randomizer will change colour evry 60 seconds, soo, 2 of the same rounds will never happen one after the other, soo im saying that

if nr == 1 and randomizer is red then
 print(” onechoosen!)
 end

if nr == 2 and randomizer is blue then...

2 answers

Log in to vote
0
Answered by
Xapelize 2658 Moderation Voter Community Moderator
1 year ago
local randomAll = workspace.randomizer
local nr = math.random(1,7)

if nr == 1 and randomAll.BrickColor == BrickColor.new("Really Red") then
           .........
end

part.BrickColor == BrickColor.new(color name) will be the formula you are looking for, and i changed the script so u can see a better example

0
thanks for the anser,i cant get it to work, mission aborted. Gigaset39 111 — 1y
Ad
Log in to vote
2
Answered by
imKirda 4491 Moderation Voter Community Moderator
1 year ago

There is a better way to do this, you can create table with your game modes and select random game mode from it, if the selected game mode is equal to the previously selected game mode you select the game mode next to it.

local modes = {
    "Classic Mode",
    "Hardcore Mode",
    "Speed Mode",
    "Crazy Mode",
    "Slow Mode",
    "Super Mode",
    "Penguin Mode"
}

Here is how to get each of the game modes:

print( modes[1] ) -- Classic Mode
print( modes[2] ) -- Hardcore Mode
print( modes[3] ) -- Speed Mode
print( modes[4] ) -- Crazy Mode
print( modes[5] ) -- Slow Mode
print( modes[6] ) -- Super Mode
print( modes[7] ) -- Penguin Mode

You can generate random number from 1 to 7 (or instead of 7 you can use #modes which is the amount of modes available) to get the random game mode:

print( modes[ math.random(1, #modes) ] ) -- Crazy Mode
print( modes[ math.random(1, #modes) ] ) -- Penguin Mode
print( modes[ math.random(1, #modes) ] ) -- Slow Mode
print( modes[ math.random(1, #modes) ] ) -- Classic Mode
print( modes[ math.random(1, #modes) ] ) -- Classic Mode (duplicate!!!)
print( modes[ math.random(1, #modes) ] ) -- Hardcore Mode
print( modes[ math.random(1, #modes) ] ) -- Super Mode

Here is a way to get rid of duplicates:

local lastMode = nil

local function getRandomMode()
    local index = math.random(1, #modes)
    local mode = modes[index]

    if mode == lastMode then
        mode = modes[index % #modes + 1]
    end

    lastMode = mode

    return mode
end

If number of previous mode is 7 and it generates number 7 again, 7 + 1 would be 8, such game mode does not exist, that's why it goes back to 1, this is what the index % #modes + 1 does:

print( 1 % #modes + 1 ) -- 2
print( 2 % #modes + 1 ) -- 3
print( 3 % #modes + 1 ) -- 4
print( 4 % #modes + 1 ) -- 5
print( 5 % #modes + 1 ) -- 6
print( 6 % #modes + 1 ) -- 7
print( 7 % #modes + 1 ) -- 1

The whole code:

local modes = {
    "Classic Mode",
    "Hardcore Mode",
    "Speed Mode",
    "Crazy Mode",
    "Slow Mode",
    "Super Mode",
    "Penguin Mode"
}

local lastMode = nil

local function getRandomMode()
    -- selects random game mode from 1 to 7
    local index = math.random(1, #modes)
    local mode = modes[index]

    -- if selected mode is same as the last mode then we select the mode
    -- next to it (instead of modes[index] we do modes[index + 1])
    if mode == lastMode then
        mode = modes[index % #modes + 1]
    end

    lastMode = mode

    return mode
end

print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )
print( getRandomMode() )

0
im soo sorry, and im sso noob at scripting, i think you wasted a lot of time to write this answer,sorry, but i dont understand nothing,cause im too noob. Gigaset39 111 — 1y
0
and i cant accept ur answer,sorry again. Gigaset39 111 — 1y

Answer this question