looking for a slow damaging brick

[PP] privacy please
Joined 07/05/2020
Posts 92
04:21 PM 01/02/2023
i just need a script for a brick that slowly damages the player when touching it


dargy
Joined 23/06/2019
Posts 4,820
05:20 AM 05/02/2023
Alright, let's make do this thing!

First, we need to register when the player either steps on or off the brick. We use the Brick.touching and Brick.touchingEnded listeners to do this. These listeners are pretty self-explanatory by their names but you can check the documentation to see what they do.

Next, we find the "slowBrick" in the map. We search through the World.bricks array to find the specific brick with that name and set it as a variable to use.

When the player touches the brick, we need to set a p.setInterval. Why a specific "p.setInterval"? We use this in scripting to ensure if the player leaves the server or is disconnected, node-hill will automatically cancel this interval instead of a normal setInterval still repeating and throwing errors that the player doesn't exist. We will put this in a player variable to use later on globally.

(1/2)


dargy
Joined 23/06/2019
Posts 4,820
05:21 AM 05/02/2023
Quote from dargy , 05:20 AM 05/02/2023
Alright, let's make do this thing!

First, we need to register when the player either steps on or off the brick. We use the Brick.touching and Brick.touchingEnded listeners to do this. These listeners are pretty self-explanatory by their names but you can check the documentation to see what they do.

Next, we find the "slowBrick" in the map. We search through the World.bricks array to find the specific brick with that name and set it as a variable to use.

When the player touches the brick, we need to set a p.setInterval. Why a specific "p.setInterval"? We use this in scripting to ensure if the player leaves the server or is disconnected, node-hill will automatically cancel this interval instead of a normal setInterval still repeating and throwing errors that the player doesn't exist. We will put this in a player variable to use later on globally.

(1/2)
^ In this p.setInterval, we need to make the player do damage. For this example, I've set the interval to be executed every 2000 milliseconds or 2 seconds. In this interval, we set the player's health using p.setHealth and subtract the player's health (p.health) by damageHealth, or in this example, 5.

Next, we need to use the Brick.touchingEnded to stop the interval. This is as simple as using the global JavaScript function "clearInterval". We clear the player's interval by inputting that variable we made before and that's it! We're done. 🎉

My version: https://gist.github.com/dragonismcode/7d14b5e18d5403690688dcb2cca02ee6

If you ever need any node-hill assistance, join the node-hill discord or take a quick look at the documentation!

Docs: https://brickhill.gitlab.io/open-source/node-hill

Hope this helps, good luck on your scripting journey! 😊

(2/2)

1