While writing code, you may have heard or used Conditions. They are an essential to processing data and code.
As in basically every (or at least I hope every) coding language there are if and else statements, these statements check if a value returns true and if it does, it will run code inside of it
Here is an example in skript:
if player has permission "test.permission":
#code
else:
#other code
In this example we are checking if the player has the permission "test.permission" using the Has Permission Syntax
In the If statement, it is checking the player's permissions, and if they have "test.permission" then it returns true. When it returns true, that means that the if statement will run the code inside of it.
The Else statement is basically a catch, if the if statement returns false (meaning the player does not have the "test.permission" permission) then the code inside the else statement will run.
In skript, you do not need to use if when doing a condition, you can just write the condition
example:
player has played before:
#code for if statement
else:
#code for else
This segment of code, is the same as using an if statement.
Although, In my own opinion, I believe using the If is better practice and mentality for coding.
Another thing with conditions in Skript is that to just check if a value is true, rather than using and if or having to indent, you can just check the condition.
Let me show you:
on join:
player has not played before
broadcast "this player (%player%) has not played before :O"
Now this segment of code checks if the player has NOT played before, by checking player has not played before
you are essentially doing a mini-if statement to check if that value is true, if the player has played before. As in the player has not played before
will return false then the broadcast statement will not run.
Skript also includes conditions called inline conditions, these use one line (hence the name inline).
These work by having an effect/expression followed by some set condition
An example would be:
on join:
broadcast "Welcome %player% to the server" if player has played before
This will do the same thing as the 2nd example, different message- but same process.
Skript also has filters, these aren't really conditions, but they filter items depending on an input filter which works like where [<some condition using input>]
This is a little confusing, let me show you an example:
on join:
send "%player% has joined" to all players where [input has permission "messages.join"]
This example takes %all players% which is all the online players, and filters it where the input value has the permission "messages.join" so you end up with just the online player with the "messages.join" permission.
In most coding languages (including Skript) there is a thing called else if
this statement goes after an if statement and will run if the condition defined is true and then acts as its own else statement, this will only run if the connected if statement is false.then it tests if the condition is true, then runs. This is useful for checking multiple of the same thing, but if used incorrectly it could cause the if statements not to run, or run incorrectly.
Here's a short example:
on load:
if 10 < size of offline players < 100: #checks that size of offline players is between 10 and 100
broadcast "%size of offline players%"
else if 1< size of offline players < 10: # checks that the size is not 10-100 and that the size is 1-10
broadcast "Only a few amount of people have joined, its too much effort to display a number >:)"
else: #size is neither 1-10 or 10-100
broadcast "WOW more than 100 people have joined, much pog"
Tip: If you have an if statement that you want to run and make 100000% sure that it does not continue to something else, then it is a good idea to use stop's
on join:
if player has played before:
send "Welcome back"
stop
else if player has not played before:
send "WELCOME!!!!!!!"
stop
else:
send "this will never happen,,,,,,, unless....."
stop
send "HI, im lonely no one sees me :("
Thank you, Hopefully this makes sense to you so you can use it in the future. I hope this helps you if you needed it.
You must be logged in to comment
Oct. 27, 2020, 3:32 p.m. - 🎃BasToTheMax🎃 ¶
I like this tutorial! It helped me very Much
(sorry for my bad Inglisch)
Nov. 3, 2020, 8:03 p.m. - BluBoy3ch0 ¶
Thanks for the review! Don't worry about your English :)
Nov. 16, 2020, 1:20 p.m. - dvdX ¶
Cool tutorial, it helped me with the first join of a player (Cool discord server too, it helped me a lot with my sripts)
Dec. 18, 2020, 3:48 p.m. - littleninjatzu ¶
thanks!
Nov. 16, 2022, 10:08 p.m. - Logixmouse ¶
Not really what I was Looking For, I am looking for a "If item name = {{name}}"