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

Multiple Conditions in an if statement

Asked by 10 years ago

I am trying to remake a RCL script to find when players have 1) More than 10 kills 2) A kills-death ratio less than lets say .72

then they are disqualified and teamed to the spectators.

How would I write that? I am having difficulties writing it. I have the leaderboard stats for an RCL. It says

function onHumanoidDied(humanoid, player)
    local stats = player:findFirstChild("leaderstats")
    if stats ~= nil then
        local deaths = stats:findFirstChild("Wipeouts")
        deaths.Value = deaths.Value + 1
            if deaths.Value>10 then
            player.TeamColor=BrickColor.new("White")
            end 

to disqualify them. I want to change it to add

if kills/deaths < .72

along with the other condition of if deaths.Value>10 to disqualify.

2 answers

Log in to vote
0
Answered by 10 years ago

In order to fully understand, you need to understand if statements and if conditions. What you would be talking about is the "or" condition.

Lets take an example:

i = 2

if i > 0 or i < 1 then
    print('True!')
end

Pretty basic, but it gets the point across. Since "i" is in fact bigger than 0 it moves on. But lets say it wasn't. Let's say i = -1:

i = -1

if i > 0 or i < 1 then
    print('True!')
end

In that case, it would then check the next statement

i<1

Is -1 bigger than 1? No? Than it doesn't move on at all.

We can apply this to your script, line 6.

local kills = stats:FindFirstChild("KOs")

if deaths.Value>10 or (kills/death) < 0.72 then
    player.TeamColor=BrickColor.new("White")
end
Ad
Log in to vote
-1
Answered by 10 years ago

I'm going to assume you meant more than ten deaths rather than kills.

This code assumes it is located within a localscript.

function onHumanoidDied()
    local stats = game.Players.LocalPlayer:findFirstChild("leaderstats")
    if stats ~= nil then
        local deaths = stats:findFirstChild("Wipeouts")
        deaths.Value = deaths.Value + 1
        local kills = stats:findFirstChild("Kills") -- assuming you named the stat that holds the kills,  "kills"
        local KD = kills.Value/deaths.Value -- Find the Kill:Death ratio
        if deaths.Value > 10 or KD < .72 then -- Put "or" if you want it to activate as soon as one condition is met. Put "and" if you want both conditions to be satisfied before activating the code.
            game.Players.LocalPlayer.TeamColor = BrickColor.new("White")
        end 
    end
end

game.Players.LocalPlayer.CharacterRemoving:connect(onHumanoidDied)

Let's examine this code line by line.

  1. Create a function called onHumanoidDied. Note: Since it's (assumed to be) in a local script, you don't need the humanoid and player arguments.

  2. Create a local variable called "stats" equal to the leaderstats instance within the player.

  3. If stats isn't nil (meaning, if something is there), then…

  4. Create a local variable called "deaths" equal to the "Wipeouts" value within the stats instance in the player.

  5. Add 1 to the deaths value.

  6. Create a local variable called "kills" equal to the "Kills" value within the stats instance in the player.

  7. Create a local variable called"KD" equal to the value of kills/deaths.

  8. If the player has died more than 10 times OR if the players Kill:Death ratio < .72 then…

  9. Change the players team to the one colored white.

10 - 12. End the function and two if statements

  1. If the LocalPlayers character is removed then call "onHumanoidDied".

I hope that helps. If you have any question feel free to comment or PM me.

Answer this question