Chloe’s Corner: Spruce Up Your World with Code Snippets

Community

Welcome back to Chloe’s Corner! Today, we will explore the exciting world of code snippets and share tips and tricks for enhancing your worlds.

As world-builders, we all know the power of code snippets in bringing our worlds to life. In this post, I will share some of my favorite code snippets that can add unique features and functionality to your World to Build world. Whether it’s teleporters, speed modifiers, or other creative uses of code, these snippets will help you take your World to Build experience to the next level. Let’s get started!

1) Teleporter

This code snippet will allow you to teleport your characters from one location to another with just a touch. Here’s how it works:

function OnInteracted(interactor)
  this.RunOnServer("Teleport", interactor)
end

function OnTouchBegin(wildcard)
  if IsCharacter(wildcard) then
    this.RunOnServer("Teleport", wildcard)
  end
end

function Teleport(character)
  character.position = GetObjectByName("Teleport Pad").position
end

To use this code, add the script to a brick (which you can name anything you like, but just make sure it’s not called “Teleport Pad”). Then, rename another brick to “Teleport Pad”. When you touch the brick with the script, you will be teleported to the brick named “Teleport Pad”. Keep in mind that this is a one-way teleport, so if you want to teleport back, you’ll need to add the same code to the “Teleport Pad” brick and rename it to another name.

The code consists of three functions: OnInteracted, OnTouchBegin, and Teleport.

The OnInteracted function is triggered when the player interacts with the brick that has this script. It calls another function, Teleport, and passes the player’s character as an argument.

The OnTouchBegin function is triggered when the player touches the brick. It checks if the wildcard, which represents the player, is a character. If it is, it calls the Teleport function with the player’s character as an argument.

Finally, the Teleport function sets the position of the player’s character to the position of the brick with the name “Teleport Pad”. The GetObjectByName function retrieves the brick with the specified name.

That’s it! By adding this code to a brick, you have created a one-way teleporter in your world in World to Build. To make it a two-way teleporter, simply add the same code to another brick and change the name of the bricks.

2) Shift to Sprint

With this code snippet, you can give your character a quick burst of speed when you need it. By holding the Left Shift key, you can double the speed of your character. This is a great way to quickly traverse large areas or run from your friends in your world. And when you release the Left Shift key, your character’s speed will return to its normal speed.

function Tick()
    if(Input.KeyPressed("LeftShift"))then
        GetLocalCharacter().speed = 2
    elseif(Input.KeyReleased("LeftShift"))then
        GetLocalCharacter().speed = 1
    end
end

In this code snippet, we’re adding a sprint functionality to your character in World to Build. The code uses the Input module’s KeyPressed function to check if the “LeftShift” key is being pressed. If it is, the character’s speed is set to 2, allowing them to move faster. If the “LeftShift” key is released, the character’s speed is set back to 1, which is its normal speed.

To use this code, you’ll need to add the script to an object in your world. This can be any object you like, or you can simply just create a blank script. When the object runs the script, the sprint functionality will be added to the local character. This means that if you have multiple players in your world, each player will have their own separate sprint function, which can be activated by pressing the “LeftShift” key.

3) No-clip

This code adds a fun feature to your world, where the player can fly around without any obstacles stopping them. To use this code, add the script to a brick in your world. Then, when a player touches the brick, they will be able to fly freely and explore your world from a new perspective. This is a great way to add a new level of excitement and discovery to your world, and can be especially useful for players who want to get a better look at your designs and structures.

function OnTouchBegin(wildcard)
  if IsCharacter(wildcard) and isHost then
    wildcard.SetNoClipMovement()
  end
end

The OnTouchBegin function is triggered when the player touches the brick. It checks if the wildcard, which represents the player, is a character. If it is, it sets the player’s no-clip movement, allowing them to fly and pass through other objects in the world.

Please note that this code only works for the player who is hosting the world, if you want it to work for every player, you can remove the “and isHost” portion of the code.

4) Messing with Gravity

The following code gives you the power to manipulate gravity with a simple touch. By touching a brick with this script, you can control the level of gravity in your world, adjusting it to have no effect or adding weight for a completely different experience. Whether you want to fly effortlessly through the air or navigate through a heavily weighed environment, this code has you covered.

function OnTouchBegin(wildcard)
  if IsCharacter(wildcard) then
    wildcard.gravityPower = 0
  end
end

This code consists of a single function called OnTouchBegin. It’s triggered when the player touches a brick that has this script attached to it. The function checks if the player is a character by using the IsCharacter function and passing the wildcard, which represents the player, as an argument.

If the player is a character, the function sets the gravity power of the player’s character to 0. This means that the player’s character will have no gravity and will be able to fly around the world. However, keep in mind that you can change 0 to any value you want, with 0 being no gravity and 1 being normal gravity.

By using this code, you can make interesting and creative effects in your worlds by controlling the gravity of the player’s character. Have fun experimenting!

5) Invincibility

The code below lets you become invincible, with a simple touch. By touching a brick with this script, you can turn on an invisible force field that protects you from all harm, making you invulnerable to anything that comes your way. Whether you want to explore the world without worrying about danger, or take on challenging obstacles with confidence, this code gives you the power to do so.

function OnTouchBegin(wildcard)
  if IsCharacter(wildcard) then
    wildcard.forceField = true
  end
end

The code contains a single function, OnTouchBegin, which is triggered when the player touches the brick that has this script.

The function checks if the wildcard, which represents the player, is a character. If it is, it sets the player’s forceField property to true, making the player invincible to damage.

With this code, you can create a brick that grants the player invincibility when they touch it. This can be useful in scenarios where you want to allow the player to temporarily become invulnerable, for example, in a challenging section of your world.

Conclusion

Thank you so much for taking the time to read this post. I hope it has been helpful in showing you how you can customize your world with a few simple code snippets. Most of the code in this post was taken from Tyster on the World to Build Discord, and I want to give a big thank you to him for sharing his knowledge and experience.

If you would like to continue learning about World to Build and how to further customize your world, I highly recommend checking out the World to Build documentation. You can read the documentation here.

Thank you again for reading and I hope these code snippets help you make your world even more enjoyable.