Skript - Beginner mistakes& How to optimize YOUR codeI'm making this post in favor of helping new developers. We'll first go over common mistakes of Skript, then I'll begin teaching you how to optimize your own code.Common Mistake #1 -Using Spaces or forgetting indentationIn Skript, all code must be Indented, 4 spaces or 1 tab each.What's the difference between tabs and spaces?A tab is 4 spaces, but backspacing on one will instantly delete 4 spaces, and pressing Tab on your keyboard instantly makes 4 spaces.It's faster, and more efficient. Some say it's inefficient in other languages, such as Java, which isn't true, languages like Java aren't too focused on indentation, not as much as Skript is, since most IDEs auto-indent for you, or all tabs are converted to spaces at the end of the day anyway.Example of bad code:
on break:send "You have broken a block!"give 1 of diamond to playersend "Gave Diamond!"The problem is ALL of this code needs to be 4 spaces (or 1 tab) exactly. You only indent your code after an 'if' or any code ending with a colon :Common Mistake #2Using .%player's uuid% instead of ::%player's uuid% or using %player%A per-player variable in Skript looks like this: {mana::%player's uuid%} And a common mistake people make is using {mana.%player's uuid%} ; using :: instead of . in your variable is better, as it will save your variables better in variables.csv; also, many players also make the mistake of utilizing %player% when they don't mean to. When using %player%, you are saving the value to the player's name, meaning, if they ever change their name, all of their progress will be deleted. Use %player's uuid% instead.Example of a good variable:{mana::%player's uuid%}Common Mistake #3Spaghetti codeHuh, wait THERE'S SPAGHETTI?No. This is spaghetti code:(Forums kinda butchers it, but you get the idea.)
command /sell:trigger:if player's held item isn't iron ingot:if player's held item isn't gold ingot:if player's held item isn't coal:if player's held item isn't a diamond:if player's held item isn't an emerald:send "Example"else:send "You can't sell emeralds!:else:send "You can't sell diamonds!:else:send "You can't sell coal!:else:send "You can't sell iron ingots!:else:send "You can't sell iron ingots!:Just so.. many useless if's. Okay, so, "Rosa! But how else am I supposed to send a custom message for each item they're not supposed to sell UVU"Simple.
command /sell:trigger:if player's held item isn't iron ingot or diamond or emerald or gold ingot or coal:send "Example"else:send "You can not send %player's held item%!"This does the exact same thing, %player's held item% will be what they're holding, so that's custom message for each item they can't sell in 12 less lines.If not planning to use an 'else', you can also do
command /sell:trigger:player's held item isn't iron ingot or diamond or emerald or gold ingot or coalsend "Example"No if's needed. Do it in any and every possible circumstance.Section II - How to optimize your codeI won't go too in-depth on this section. Just a bunch of snippets of tricks I've learned over the years.Tip #1Delete as many variables as possibleWhen you're done using a variable delete it it's simple, really. Say {mana::%player's uuid%} is your variable. In any event or command where need be, just put delete {mana::%player's uuid%} now I DEFINITELY wouldn't recommend deleting important variables, but if you're only using the variable for a temporary, short task, delete it after use, or delete it when the player leaves.This leads us to local variables all local variables start with '_', for example: {_timer::%player's uuid%} or {_t} are local variables. Once the code stops, they're automatically deleted, they'll never be saved in variables.csvUse these when you need to save something or use a variable in code just for that one piece of code.Tip #2Use RAM variables, PLEASE!What are RAM variables? Well, when your server stops or restarts, they're automatically deleted. A RAM variable always begins with '-' or whatever you set it to in config.yml as this isn't in normal Skript, you need to enable this in the config.ymlThis article will show you how to make any variable starting with - delete on restart - https://forums.skunity.com/threads/skript-variables-ram.5865/So you have your RAM variables set up. This is what they look like: {-thirst::%player's uuid%}They are EXTREMELY useful for saving values like a Thirst meter, or whatever other use you can think to use them for. Once they're set up, just start any variable with '-' to make it delete the variable upon server restart. This keeps variables.csv from clogging up with useless variables that don't need to be saved beyond a restart.
Tip #3Uh.. Why are you looping players for your scoreboard?One of the most useful tips for any Minehut server. DON'T loop players for your scoreboard, there is a infinitely better way, and it only requires SkBeeThis is an example of my method. Make your scoreboard just like mine, and put it in a function.
function score(p: player):set {_uuid} to {_p}'s uuidset title of {_p}'s scoreboard to "&lPIE&lDOMS"set line 11 of {_p}'s scoreboard to "&l[/pie] "set line 10 of {_p}'s scoreboard to "&7"set line 9 of {_p}'s scoreboard to "●&f Coins&8 |&f %{_p}'s balance%"set line 8 of {_p}'s scoreboard to "&7"set line 7 of {_p}'s scoreboard to "●&f Kills&8 |&f %{stats::%{_uuid}%::kills}%"set line 6 of {_p}'s scoreboard to "●&f Level&8 |&f %{stats::%{_uuid}%::lvl}%"set line 5 of {_p}'s scoreboard to "●&f Pies Baked&8 |&f %{stats::%{_uuid}%::pies}%"set line 4 of {_p}'s scoreboard to "&7"set line 3 of {_p}'s scoreboard to "&lSupport us - [/buy]"set line 2 of {_p}'s scoreboard to "&5"set line 1 of {_p}'s scoreboard to "&f{ /discord&f | /pie&f |/profile&f }"toggle {_p}'s scoreboard onWhenever you update a value on the scoreboard, in your code put score(player)
on join:score(player)Mini-tip BONUS section!Tip #4 - Use functions WHENEVER you can, do not underestimate their capability. For example, I needed to make 10+ Shop GUIS for my server. I made a function that allows me to make any GUI I want in one line of code, using a function with around 15-20 lines.Tip #4 - Disable backups in config.yml so that your server doesn't endlessly produces hundreds of thousands of backups overtime; usually resulting in the server dying whenever it's started.
Tip #5 - Always use these useful inputs instead of using 'loop all players' -
send "%player% executed command&e %full command%" to all players where [{-cspy::%input's uuid%} is true]send "%player% >> %message%" to all players where input has permission "Staffchat"]
PREV: How to Enable Apache Virtual Host in Windows 10. - Linux ...