#how to loop something / make it run multiple times / make it run for multiple seconds

1 messages · Page 1 of 1 (latest)

fading moat
#

What I'm trying to do is, if i have a certain tag and am in a 5 block radius of any entity, it removes a certain effect from them. What i want to do is: If I get close to them, it detects it like usual but instead of removing it once, it runs it multiple times or if possible for 3 seconds

next torrentBOT
#
Someone will come and help soon!

💬 While you wait, take this time to provide more context and details.

🙇 After a while, hit the Summon Helpers button to ping the helper team. They'll be happy to help you

✅ Once your question has been resolved (or you no longer need it), please click Resolve Question or run /resolve

hollow meadow
#

I see what you tried to do, but Minecraft functions are basically just commands in a text file.

fading moat
#

oh

hollow meadow
#

They don't work like most programming languages

fading moat
#

crap

#

So are there any work arounds for it using MC Commands?

hollow meadow
#

Sure

#

One question, is the function running as and at the player?

fading moat
#

i believe so yeah

#

heres the code without the for loop stuff

#

If i dont reply i have to really quickly do something irl thats why

hollow meadow
fading moat
#

Its an effect from a different mod

#

Its similar to minecraft:regeneration

hollow meadow
#

ah

fading moat
#

Sorry shouldve told you earlier

#

My fault

hollow meadow
#

Did I understand that right, that for every entity that enters that 5 block radius, the effect clear command should run 3 seconds?

fading moat
#

Yeah

#

So for 3 seconds it would constantly clear effects

#

Even if they get out of the 5 block radius

#

I tried to use a for loop as u saw but didnt really work lmao 😂

hollow meadow
#

Why do you need it to run for 3 seconds?

fading moat
#

Im trying to make it so in combat it cancels healing

#

But the issue is

#

The mod im using it with knocks the player back a lot so it doesnt have much effect

#

I was thinking if it would run it 3 times or for 3 seconds itd have more of an effect and actually stop healing

#

Idk if i can explain it well enough

hollow meadow
#

What you need:

  • a scoreboard that acts like a timer for seconds
  • a schedule function that runs every second to reduce the timer.
  • a tick function that sets the score of every player near you
#

Do you already have a tick and load function?

fading moat
#

Yeah i have tick and load

#

Load does tellraw every time i reload/load the datapack

#

Scoreboards is a good thing i need to learn them anyways so its good its a must for the project haha

hollow meadow
#

First you need to add a command in the load function that adds a dummy scoreboard to your world.

scoreboard objectives add levent.heal_block_timer dummy 

this adds a new dummy scoreboard objective (dummy means, it is not linked to a statistic) with the name levent.heal_block_timer

#

next, we add a command to the tick function, that sets the score of every player that is near another player with the Sakura tag to 3.

execute at @a[tag=Sakura] as @a[tag=!Sakura,distance=..5] run scoreboard players set @s levent.heal_block_timer 3
#

Add a second command to the tick function that clears the effect as long as the player has a score over 0.

effect clear @a[scores={levent.heal_block_timer=1..}] <effect id>
fading moat
#

Oh crapp

#

Yo thank you so much man wth

#

I love this community already 😭

#

Ill put them in as soon as im back on pc

hollow meadow
#

Last thing, create a new function. In there, add a command that reduces the timer score by one

scoreboard players remove @a[scores={levent.heal_block_timer=1..}] levent.heal_block_timer 1

and a second command that calls this function again in 1 second

schedule function <the name of this function> 1s
#

On its own, this function will do nothing, so to start the cycle call this function once in the load function.

fading moat
#

thats all?

hollow meadow
#

yep

fading moat
#

Alright thank you

#

I'll let you know if i dont get anything

#

tysm again

hollow meadow
#

It's past midnight where I live, so I might not be here to help you today if you get stuck.

fading moat
#

Same lol

#

real quick question

fading moat
hollow meadow
#

the names can be whatever you want them to be. limitations are [a-z,0-1,_]

#

no spaces or capital letters

fading moat
#

Alright

#

i think it should work

hollow meadow
#

don't forget to save and do /reload

fading moat
#

is the scoreboard supposed to be visible\

#

i dont see anything so im assuming not

hollow meadow
#

only if you set its to be visible on the sidebar

fading moat
#

alright

hollow meadow
#

scoreboard objectives setdisplay sidebar <objective name or no name to remove it>

fading moat
#

not sure if i loaded it in correctly

#

one second

hollow meadow
#

that's the scoreboard type. The objective name is levent.heal_block_timer

fading moat
#

oh crap

#

sorry for the rookie mistakes lol im completely new as i said

hollow meadow
#

nw

#

we all have been there at some point

fading moat
#

honestly

#

im gonna leave it be how it is

#

ill test it tomorrow if i can and i think thats best for both of us

#

it's late for both of us so i dont wanna keep you up lol

hollow meadow
#

I'll finish my project first anyways

fading moat
#

well, regardless i'd be hindering you lol

#

once again tysm i'll let you know tomorrow if i cant get it to work lol

#

ill test around with it a bit more rn

hollow meadow
#

👍

fading moat
#

Yeah I changed the name because apparently the objective cant be bigger than 16 characters n stuff so i changed some stuff around, pretty sure it works but i'll hit you up tomorrow for sure. have a great night tysm for the help!!

native delta
tawdry wave
fading moat
#

Im making this in 1.16.5

#

Just for information @@

tender quailBOT
#
⚙️Raycast

If you've not made many datapacks, you might not be familiar with raycasting. It's a really useful tool which we can use to essentially find where the player is looking and then do stuff (such as spawn explosions 😎).

To create a raycast, we can use a recursive function. A recursive function is a function which will run itself over and over again. In our case, the function will run itself 0.1 blocks forward (^ ^ ^0.1) each time, and repeat until it hits something. It will do this all within the same tick - but bear in mind that the bigger the distance, the more laggy it will be. We can stop this unwanted behaviour by putting a distance limit on the raycast - each time it runs itself, it will count down on a scoreboard, and only repeat if it hasn't hit the limit.

start_raycast function:

# Set the distance limit on the raycast. (10 x limit in blocks, so 1000 would be 100 blocks)
scoreboard players set .limit <any objective> 1000

# Start the raycast
execute at @s anchored eyes positioned ^ ^ ^.1 run function <namespace>:raycast

raycast function:

# Remove one from the limit
scoreboard players remove .limit <objective> 1

# Optional - place a particle, to make the raycast leave a trail
particle minecraft:dust 1 0 0 1 ~ ~ ~

# If the raycast has hit a block, do something
execute unless block ~ ~ ~ #<namespace>:pass_through run setblock ~ ~ ~ diamond_block

# If the raycast hasn't hit a block, continue, but only if the limit is 1 or more (1..)
execute if block ~ ~ ~ #<namespace>:pass_through positioned ^ ^ ^0.1 if score .limit <objective> matches 1.. run function <namespace>:raycast```

namespace/tags/blocks/**pass_through**.json:```
{
    "replace": false,
    "values": [
        "minecraft:air",
        "minecraft:cave_air",
        "minecraft:void_air"
    ]
}

This should be it! Make sure you add all the functions, and fill in all the blanks (indicated by <this>). Then, you can run the start_raycast function as a player, and it should work.

fading moat
#

yo Hedean

#

it wont work

#

It goes from 3 to 2 and then goes right back up to 3 even if im far away from them, i dont wanna ping u cuz yeah but hopefully u see it 🙏

fading moat
hollow meadow
#

Fixed

fading moat
#

love you

#

my real goat

#

whats wrong here?

#

nvm it works

#

i love you so much no homo

hollow meadow
hollow meadow
fading moat
#

say it back

hollow meadow
#

I love you too no homo

fading moat
#

hey i got more issues for you to solve 😂

#

the predicate im trying to use

#

is sneak.json has this:

{
"condition": "minecraft:entity_properties",
"entity": "this",
"predicate": {
"flags": {
"is_sneaking": true
}
}
}

#

im trying to make it so you get regen 1 until you stop crouching basically

#

wake up Hede

#

😭

hollow meadow
#

I was driving my parents to a party. I'll help you in a minute.

fading moat
fading moat
#

and it still said the same stuff

#

let me re-make it

fading moat
hollow meadow
#

What does the log say?

fading moat
#

smart

#

i already have them

fading moat
#

i think this is from the old one

#

yeah

#

the current files called "reg.mcfunction"

hollow meadow
#

in the logs, it should show an error why it doesn't work

tender quailBOT
#
:information_source: Default Launcher Logs

The logs are where Minecraft displays errors when something goes wrong and can thus help you gain information about why something isn't working for you!
To open the logs:

  1. Enable logs in the Minecraft Launcher
  2. Start your game (or restart it if you already have an open instance)
  3. Enjoy spotting errors getting much easier!
fading moat
#

time to restart

#

💀

#

oh...

#

wait..

#

nvm

#

i was gonna say i forgot to /reload but i didnt

#

oh crap

#

it worekd?

#

worked* i think?

#

i did /function the name of function

#

im crouching but it doesnt work again

#

hm one sec

#

ill get logs to open

#

Alright re-launching MC

#

opening world

#

crouching does nothing in logs @hollow meadow

#

i did /function reg

hollow meadow
#

The error should occur when mc is parsing the files further up in the logs (if there is an error)

fading moat
#

oh

#

theres yellow text, but its either other mods or completely unrelated stuff

#

i dont think the codes giving out errors

hollow meadow
#

could you send the pack, so I can take a better look?

fading moat
#

Yup

#

one second

#

Sorry for bothering a lot btw lol

#

if you're busy with anything else just drop this

hollow meadow
fading moat
#

i could delete cut.mcfunction honestly since the code you made already replaces it

#

I've seen other people use scoreboards to detect sneaking but idk

#

this should theoretically work

#

oh crap

#

i found the "problem"

#

im just stupid augh

#

i put a repeating command block down and did /function function

hollow meadow
#

Does it work now?

fading moat
#

yeah

hollow meadow
#

🎉

fading moat
#

i would need to put it into load.mcfunction or something to avoid having a repeating command block right?

hollow meadow
#

You mean the timer score reduce function?

fading moat
#

oh nvm

#

i meant

#

you know how i need to put the /function reg in a repeating command block for it to be active 24/7.

I just need to put it in tick.mcfunction right?

hollow meadow
#

yes

fading moat
#

yeah alright

#

one last issue 😭

#

the thing with the regen effect is when its a repeating command block or a tickmcfunction it constantly gives me regen which stacks (in a way) so even if its regen 1 it heals you to max HP instantaneously

hollow meadow
#

the tick function executes every tick (20 times a second)
the load function executes once when loading the world or using /reload

hollow meadow
fading moat
#

like how do i give inf duration

#

what number would it be?

hollow meadow
#

you don't put it into the tick function. Use an advancement with the tick condition. from the image, it looks like the effects already have infinite duration.

#

Yust make sure to revoke the advancement after the player dies

hollow meadow
fading moat
hollow meadow
hollow meadow
tender quailBOT
#
**📖 Misode**

Awesome website with generators for almost everything. Also contains a technical changelog for new versions, guides on how to use some features, a performance report inspector and more!

Full list of generators: Loot Tables, Predicates, Item Modifiers, Advancements, Recipes, Text Components, Damage Types, Chat Types, Trim Materials, Trim Patterns, pack.mcmetas, Dimensions, Dimension Types, Biomes, Configured Carvers, Configured Features, Placed Features, Density Functions, Noise, Noise Settings, Structures, Structure Sets, Processor Lists, Template Pools, World Presets, Flat World Presets, World Settings, Block Tags, Entity Type Tags, Fluid Tags, Game Event Tags, Item Tags, Biome Tags, Structure Tags, Blockstates, Models, Fonts, Atlases

Link: https://misode.github.io/

fading moat
#

hold on i gotta lock in and stop the music for this 😂

hollow meadow
fading moat
#

{
"criteria": {
"requirement": {
"trigger": "minecraft:tick",
"conditions": {
"player": {
"nbt": "Tags:["Sakura"]"
}
}
}
},
"rewards": {
"function": "levent:subskills/reg"
}
}

#

i got this

hollow meadow
#

The function that will be rewarded, has all the effects the player is meant to get

fading moat
#

i have to make my own advancement right like

#

and then put the code above in there right?

hollow meadow
#

yes

fading moat
#

To add advancements, first create a folder named advancements inside the namespace folder. Then, create a file named (advancement_name).json (You can't put spaces in the file name. Use lowercase letters in the file name). in this folder or in any of its subfolders. This will be your advancement file. Your advancement will be named in the game as (namespace):(name) or (namespace):(subfolder1)/(subfolder2)/.../(name) if the file is located in a subfolder.

#

1 second

#

idk why but VSC is being reaaaly slow with creation today

hollow meadow
#

?

fading moat
#

its creating files slower than usual

#

and scripts

#

one sec advancement files here

#

i created a file named "toji.json"

#

and ill put the code in there

#

when they have the advancement theyd get the regen once and if they stop crouching the advancement gets removed correct?

#

(theyd get the advancement once they crouch)

#

if i understood everything correctly that's what we're aiming for rn

#

oh fixed it

#

i copied it wrong mb

hollow meadow
#

The advancement executes the function as soon as the player has the Sakura tag. You have to manually revoke the advancement with advancement revoke @s only <advancement path>

fading moat
#

hm okay

hollow meadow
#

If they stop, should the effects go away?

fading moat
#

yeah

#

exactly

hollow meadow
#

ok, that makes it easier than I thought

fading moat
#

thank god

#

ok the infinite regen works

hollow meadow
#

You need two advancements and two functions

fading moat
#

the regen isnt healing me though

fading moat
hollow meadow
#
{
    "criteria": {
        "requirement": {
            "trigger": "minecraft:tick",
            "conditions": {
                "player": {
                    "nbt": "Tags:[\"Sakura\"]",
                    "flags": {
                        "is_sneaking": true
                    }
                }
            }
        }
    },
    "rewards": {
        "function": "minecraft:regen function"
    }
}

this is ow the advancement for giving the effects should look.

The function then gives the effects and revokes the advancement that removes the effects.

#

The remove advancement and function are just the inverse of that.

#

I forgot something, fixed the json

fading moat
#

so

#

removing the advancement would look like this right:

advancement revoke @s levent:advancements/toji

hollow meadow
#

you don't need to include the advancements folder in the path. Mc automatically recognizes files in that folder as advancements.

fading moat
#

so just levent:toji

hollow meadow
#

yes

fading moat
#

yo mb if i mess some easy stuff up this is a lot at once

#

i got my music on pause 😂

fading moat
#

this is the one that gives the effects and revokes the advancement

hollow meadow
#

yeah

fading moat
#

alright one second

#

VSC being slow asl

hollow meadow
fading moat
#

yeah so the one that stops regen

#

alright i think i got it

#

ill come back here later with news of it workin or not

#

(like 5 mins cuz of how slow vsc is being)

#

alright

#

about to test it

#

alright i messed something up apparently

#

im gonna finish my food n take a mini break and then come back 😭

#

quick question

#

{
"criteria": {
"requirement": {
"trigger": "minecraft:tick",
"conditions": {
"player": {
"nbt": "Tags:["Sakura"]",
"flags": {
"is_sneaking": true
}
}
}
}
},
"rewards": {
"function": "levent:subskills/reg"
}
}

#

is this advancement automatically given when the player crouches or how are they supposed to get i

#

it*

hollow meadow
#

Advancements are granted as soon as all conditions are met.

#

int this case, if the player has the Sakura tag and is sneaking

fading moat
#

i dont know if the game sees the advancement

#

i tried sneaking and unsneaking and then even ran /advancement revoke levent_34 only levent:toji

#

but it just says "unknown advancement"

#

i mightve messed up while setting the advancement folder up maybe

hollow meadow
#

if it isn't listed in the chat then there is something wrong with it. Broken advancements are usually displayed in the logs.

hollow meadow
#

When you work with datapacks and mods, I suggest always having it open

fading moat
#

You're right

#

ay i think im gonna work on this tomorrow or maybe later

#

tysm for the help ill come back again if i need help lmao 😂

#

actual goat tho

hollow meadow
#

I'll be here and help, It's weekend, so I might be awake till 6 am or some stupid time like that

fading moat
#

damn man

hollow meadow
#

hj/

fading moat
#

get some rest 😂

#

oh

fading moat
#

heres the file btw

#

i prob wont work on it today cuz i cant be asked to figure out the issue at midnight 😭

#

hopefully ur on tomorrow lol

hollow meadow
#

Does the reverse_curse_technique effect affect every entity or just players?

fading moat
#

like

#

its basically binded to a key

#

and if you hold the key down it'll give you the effect

#

and certain entities (mobs) can use it

hollow meadow
#

I'll add a tag, that excludes non living entities, ok?

fading moat
#

i forgot to work on it btw ty for reminding

fading moat
#

the RCT stuff works fine btw

#

its just the crouch healing that isnt working rn

hollow meadow
fading moat
#

i mean

#

send the new file

#

i meant to say the latter, definitely

fading moat
fading moat
#

and at the time i hadn't tested if the count down was working yet so i turned it back into @a and tested it with some friends and after i fixed it i never tried it again with @e

#

idk if thats helpful to you

hollow meadow
#

Here you go. I added a few comments to explain stuff.

hollow meadow
#

Btw if you ever need some tags in the future:

tender quailBOT
fading moat
#

Hede

#

i got bad news...

#

the server i was making this for is updating to 1.20

#

do i have to re-code everything or can i just import it into 1.20

#

what is this btw

#

yo wth this man put notes down on everything 😭

hollow meadow
#

oh, that is something I added but removed, because I had a thinking error

fading moat
#

actual goat

hollow meadow
#

change it to #taglib:non_living

fading moat
#

alright

#

you mean non_living right

hollow meadow
#

yeah

fading moat
#

alright fixed

#

actual goat

hollow meadow
fading moat
#

👍

#

idk if the regens working

#

one sec

#

i think its working

hollow meadow
fading moat
#

minecraft regens so confusing

fading moat
#

yeah regen is working

#

im gonna put it on regen 2 to see the effect clearer

#

ily actual goat

hollow meadow
#

mc_goat <= me irl

fading moat
#

ong

#

me to you

#

(im glazing)

#

Hede

#

i could add slowness too right

#

testing it

#

out

#

rn

#

works

#

ily actual goat

stray marten
fading moat
#

👍