2

I need help with /testfor command!

Olibongo's Avatar Olibongo6/17/18 11:40 am
2 emeralds 1.2k 7
6/17/2018 9:04 pm
SUPERIONtheKnight's Avatar SUPERIONtheKnight
so i have been making a map and need a command block to do something when a player steps on a block.
currently it is:
/testfor @e[type=player]

but i want to make it only detect players when they step on a certain XYZ coord
and when they do it will detect the player and place a redstone block using /fill on an other block
please help as i have tried for about an hour and none of my attempts have worked :(


Posted by Olibongo's Avatar
Olibongo
Level 13 : Journeyman Architect
10

Create an account or sign in to comment.

7

Olibongo
06/17/2018 4:27 pm
Level 13 : Journeyman Architect
Olibongo's Avatar
ok i found out how to do it!
thanks for the help!
1
garlicbreathinator
06/17/2018 3:05 pm
Level 31 : Artisan Engineer
garlicbreathinator's Avatar
As mentioned below, you can detect when a player is within a radius of a coordinate position with /testfor. However, that will test as true for everyone, no matter who steps on the block. This works for some situations, but will screw you up in others. If you want to detect if any player is stepping on a specific block, go ahead and use /testfor.
Some examples of using /testfor, click to reveal
To detect if any player is in a small circular area around the point with the coords x y and z (red values are variables). Note that this assumes you haven't already center-corrected the coords and that your point is on the surface, not underground.:
/testfor @a[x=x.5,y=y.5,z=z.5,r=1]

To detect if any player is standing within a the block at coords xyz, (xyz being the block above the block you're testing for players on) you want this instead:
/testfor @a[x=x,y=y,z=z,dx=<x+1>,dy=<y+1>,dz=<z+1>]

However, there are better ways to do this, maybe by using /execute.



For example, you could use this to set a redstone lamp when any player stands on coal ore.

/execute as @a ~ ~ ~ detect ~ ~-0.5 ~ coal_ore 0 setblock <x y z> redstone_block
There are so many ways to do this that I can't list them all. If you need anything else, let me know what you need this for so I can figure out what you need.

Good luck,
Garlicbreathinator
1
Isz
06/17/2018 2:26 pm
Level 46 : Master Architect
history
Isz's Avatar
No you don't need tags like the other guy is saying, that overcomplicates something simple. You need to put x y z and radius values in the selector part. For example /testfor @a[x=4,y=33,z=23,r=1]

Using a radius of 1 will only test for a player inside that one block. Make sure the y value is not the value of the block in the ground but one above, so it can actually detect a player inside it.

Then put a chain command block afterwards with whatever command you want triggered
2
SUPERIONtheKnight
06/17/2018 2:42 pm
Level 29 : Expert Engineer
history
SUPERIONtheKnight's Avatar
You can do this, but again it isn't multiplayer friendly and it's bad practice. An example, lets say I want to give everyone a single item within that area. /testfor isn't capable of doing this without a comparator(which causes lag).

~SUPERIONtheKnight
1
Isz
06/17/2018 8:13 pm
Level 46 : Master Architect
history
Isz's Avatar
It is multiplayer friendly, you wouldn't use testfor for a give command you'd just use give command with the same coords in the selector
1
SUPERIONtheKnight
06/17/2018 9:04 pm
Level 29 : Expert Engineer
SUPERIONtheKnight's Avatar
If I do that and put it on a clock, I will get multiple items whereas in the above example we only want one. /testfor is a way around that, but players will have to wait for everyone else to leave the area in order to get their items.

~SUPERIONtheKnight
1
SUPERIONtheKnight
06/17/2018 1:03 pm
Level 29 : Expert Engineer
SUPERIONtheKnight's Avatar
Note: I'm assuming you are using 1.12.2. If you need commands for a different version, be sure to let me know. :)

First off, I wouldn't use /testfor. It isn't multiplayer friendly, and there isn't anything it can do that other commands can't. 1.13 is also doing away with the command altogether.

In this case, tags are exactly what you need. They are basically like giving a second name to an entity that we can use to select it. They have nothing to do with the scoreboard like the command itself suggests. I have no idea why Mojang added them to the /scoreboard players command, it makes it more confusing than it really is. :P

Anyways, on with the commands! :D
/scoreboard players tag @a[tag=area] remove area
/scoreboard players tag @a[tag=!area,x=#,y=#,z=#,dx=0] add area
/execute @a[tag=area] ~ ~ ~ Command here
The # after xyz should be replaced with the coordinates you are wanting to check, the rest is self explanatory I hope. Although I will also mention that you should use different tags for checking different areas(for example, use the tag "area" to check 0 0 0, and the tag "area2" to check 100 10 100).

I hope this helps! :)
~SUPERIONtheKnight
1
Planet Minecraft

Website

© 2010 - 2024
www.planetminecraft.com

Welcome