Two problems with one block of code (Random is the same and ifs are not doing anything when condition is met)

47 Views Asked by At

Contains a bit of discord.js code, but it's explained

What I'm trying to do is make it so when a user uses a specific command, it starts a random variable, which is used to add a random enemy. When I use the command though, it gives me the same number (Which used to be 4, but after changing my code a little bit, it is now 1), and doesn't do what that number is supposed to do (The if statement does not start)

My code that I think is having the problem:

        var e = Math.floor(Math.random() * 3) + 1;
        if(e = 1){
            userData.skeletons + 1;
        }
        else if(e = 2){
            userData.skeletons + 1;
        }
        else if (e = 3){
            userData.minotaurs + 1;
        }              
        else if(e = 4){
            userData.skeletons + 1;
        }
        console.log(e)

The complete code:

if (message.content.startsWith(prefix + "search")) { //if the message the 
//user sends is equal to my prefix (bp!) plus the word search, it runs the 
//below code
    if(userData.stamina >= 3){
        userData.stamina - 3
        var e = Math.floor(Math.random() * 3) + 1;
        if(e = 1){
            userData.skeletons + 1;
        }
        else if(e = 2){
            userData.skeletons + 1;
        }
        else if (e = 3){
            userData.minotaurs + 1;
        }              
        else if(e = 4){
            userData.skeletons + 1;
        }
        console.log(e)
        message.reply('To see if ya found anything, use bp!enemies')
        //replies to the user who used the command with the above message
    }
    else{ //this else is for if they do not have enough stamina
        message.reply('You require at least 3 stamina to do this!')
        console.log(`${message.author.id} You require at least 3 stamina to 
        do this!`)            
    }
}

I've created comments for those who need a bit more understanding of the code and the discord.js code.

I've checked my userData variables (variables that are unique to each user), and they're all right. I've looked through other questions, but it didn't look like they had a solution Thank you for taking your time to read this.

1

There are 1 best solutions below

1
On BEST ANSWER

Many problems with this code. if(e = 1) should be if(e == 1) and userData.stamina - 3 should be userData.stamina -= 3.

userData.skeletons + 1 should be userData.skeletons++