My game crashes every time I use While True

Started by UAPvP on

Topic category: Help with Minecraft modding (Java Edition)

Joined Oct 2023
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
My game crashes every time I use While True

So, I need to always check if player is below y0  and there is my custom block in the world.

But every time I use While to check that, my mc just crashes.

My code:
https://imgur.com/kj6psTi


My mod idea:
Custom block, when clicked starts checking if player is falling in void below y0 and is there a custom block, and teleports player above the block if so.

 

Joined May 2022
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
You may want to watch some…
Thu, 01/25/2024 - 13:16

You may want to watch some tutorials to get a better idea of how procedures work. (I recommend NorthWestTrees Gaming), but I can offer some basic pointers:

  • 'While' loops can VERY easily crash the game, especially if you're not super familiar with how they work. You're right in that a while loop will check something constantly until its condition is no longer true. But if you use a while loop and either don't give it an end condition, or give it an end condition that can't be met, then you have a problem.

    Essentially, procedures are supposed to run instantly, in a single game tick unless you tell them otherwise. A while loop would be used for something like moving the player until they're outside a certain area, running through a series of blocks to check for something, that sort of thing. But if the while loop never stops, the game can't move on to the next tick until it's finished, which usually causes a crash. For your block, you want something that will trigger every tick, not something that will keep running through a single tick until it reaches a stopping point.

  • You also need to think about what dependencies you're using. You want to make a block which, when clicked, will teleport the player back to that block if they jump in the void. The problem is that the block's update tick only gives you dependencies related to the block, (the block's position, the world the block is in, the block's current state, etc), and the player's update tick only gives you stuff related to the player, (the player's health, their position, etc.) You also have to keep in mind that, depending on where the player is, the position of the block might not even be loaded. The game does not automatically know all this stuff, so you have to be specific about what you want. 
  • The way to do this, then, would be using two procedures. The first procedure should trigger when the player right clicks the block, and should set three player-persistant variables, (I'll call them anchor_x, anchor_y, and anchor_z), to the position of the block they just clicked on. (You can make variables in the main workspace tab, look for the options on the left hand side.) Then, make a second procedure with the 'on player update tick' global trigger. (This will trigger every update tick for the player). This second procedure should check if the player's is in the End, and their current position is zero. If both are true, it should change the player's location to these three variables.