When coding your own content, it's important to try to think like a computer. That means that you must take your goal (for example, "a timer that makes the player die when 0 is reached") and rephrase it into something that makes sense for a computer. You're basically turning it into simple instructions.
The simplest ways to communicate with a computer through code are conditional statements and variables.
So, for example, the goal above would also be something like: "There is a timer. It counts down. Once it has reached zero, kill the player."
These sentences are 4 statements that are translatable into code one-by-one:
Code: Select all
local timer = 500 -- There is a timer. Initialize a variable called "timer" to 500.
function onTick()
timer = timer - 1 -- that counts down. Remove 1 from the timer every frame. 64 frames in a second. It'll reach 0 after like 5 seconds.
if timer <= 0 then -- once it has reached zero.... (if-statements are somewhat easy to read in english. like, "if timer is less than or equal to 0 then...")
player:kill() -- kill the player!
end -- functions and if statements usually have their content indented. when the conditional or function ends, an end must be placed to let the program know
end
Hope this makes sense. Being able to get into this mindset requires a bit of knowledge of the systems you can use (for example, player:kill() is quite specialized). If you're unsure about how something could be done, we can always point at the resources and provide the reformatted sentence.