I'm making a hide and seek game with my friends and i wanna make it so when we're playing a tag mode. Whenever the seeker hits the hider, the seeker becomes a hider and the hider a seeker. I already have the 2 teams set up Hiders
and Seekers
3 Answers
You could fairly comfortably use custom advancements in a datapack to do this.
You have one advancement that checks for a hiding player being hit by a seeking player, then grants a reward function that makes them switch teams and resets the advancement to be used again.
IMPORTANT: Due to how minecraft advancements work, we need to delay the swapping of teams to the next tick, because otherwise one of the advancements will run first, run its corresponding function and the other advancement will not have its conditions met anymore.
foo/advancements/hit_by_seeker.json
{
"criteria": {
"requirement": {
"trigger": "minecraft:entity_hurt_player",
"conditions": {
"player": {
"team": "Hiders"
},
"damage": {
"source_entity": {
"type": "player",
"team": "Seekers"
}
}
}
}
},
"rewards": {
"function": "foo:hit_by_seeker"
}
}
foo/functions/hit_by_seeker.mcfunction
advancement revoke @s only foo:hit_by_seeker
tag @s add Seekers
schedule function foo:swap 1t replace
And then another one that checks for the seeker hitting a hider to have the reverse effect:
foo/advancements/hit_hider.json
{
"criteria": {
"requirement": {
"trigger": "minecraft:player_hurt_entity",
"conditions": {
"player": {
"team": "Seekers"
},
"entity": {
"type": "player",
"team": "Hiders"
}
}
}
},
"rewards": {
"function": "foo:hit_hider"
}
}
foo/functions/hit_hider.mcfunction
advancement revoke @s only foo:hit_hider
tag @s add Hiders
schedule function foo:swap 1t replace
And finally, the function to actually swap the teams.
foo/functions/swap.mcfunction
team join Hiders @a[tag=Hiders]
team join Seekers @a[tag=Seekers]
tag @a remove Hiders
tag @a remove Seekers
-
do i just put them in a command block and run it once or do i have to run it multiple times? Commented Mar 9, 2023 at 9:56
-
@Rosy-senpai no commandblocks needed for this one - these are 4 different files inside a datapack. In fact, it won't work with just commandblocks, this is explicitly a datapacks answer. Commented Mar 9, 2023 at 11:41
-
How do i make these files? Just put it in like a txt file or something? Have never coded like this before Commented Mar 9, 2023 at 11:46
-
You need to make a Datapack for this to work. Here is a tutorial on how to do that. Don't worry, a datapack isn't complicated, it's just a few folders and a text files inside the world. Then you add the above files as text files (in the example
foo
is the namespace, so it goes inside thedata
folder) Commented Mar 9, 2023 at 12:17 -
Since Teams are annoying to use with commands, I like to use a scoreboard to track the teams, and use the scores to switch teams. Something like a Team
scorebaord, where 0 is Hider and 1 is Seeker. Then two commands that set the teams which alawys run.
execute as @a if score @s Team matches 0 run team join Hider @s
execute as @a if score @s Team matches 1 run team join Seeker @s
Aside from this, we will need a scoreboard to track damage_dealt and damage_taken. We will want to check whenever a Seeker dealt damage, then check for the closest Hider who has taken damage and tag them. We then use the tagged Hider to switch the Seeker's team and itself. With datapacks, it wouldn't be needed to tag them, but since these are commands blocks, it is needed.
First the scoreboards:
scoreboard objectives add dmg_dealt stat.damageDealt
scoreboard objectives add dmg_taken stat.damageTaken
The logic:
execute as @a[scores={Team=1,dmg_dealt=1..}] at @s as @p[scores={Team=0,dmg_taken=1..}] run tag @s add Hit
execute as @a[tag=Hit] as @p[scores={Team=1,dmg_dealt=1..}] run scoreboard players set @s Team 0
execute as @a[tag=Hit] run scoreboard players set @s Team 1
execute as @a[tag=Hit] run tag @s remove Hit
execute as @a run scoreboard players set @s dmg_taken 0
execute as @a run scoreboard players set @s dmg_taken 0
This should work with multiple Seekers. These commands are currently untested, so please let me know if something goes wrong!
-
so im trying this and the scoreboard already fail. The
stat.damageDealt
doesnt exist, or atleast it gives me an error Commented Mar 10, 2023 at 12:53 -
You might need to do the full path. Something along the lines of
minecraft.custom:minecraft.damage_dealt
. You can use the tab function in chat for auto completion and pardon the mistake! Commented Mar 12, 2023 at 0:38
What I would do is give the seekers strength and when a hider dies turn them into a seeker like:
scoreboard objectives add isdead deathCount
execute as @a if score @s isdead matches 0.. run function namespace:functionname
And in that function, you set the scoreboard value back to 0 and do whatever stuff you want it to do for the team switching.
minecraft:damage_taken
stat.