How to /testfor a player's gamemode

How to /testfor a player's gamemode - Person in Black Pants and Black and White Nike Basketball Shoes

I've recently had some difficulties finishing a system; in fact, I need to /testfor myself if I'm on spectator mode or another gamemode. Because I didn't know what to write, I tried this, but it didn't work:

/testfor Kimatuy {gamemode:3}

Can you help me with this?



Best Answer

To complete what aytimothy said, `@a[m=M] targets all Players with gamemode M:

0 for Survival, 1 for Creative, 2 for Adventure, 3 for Spectator. For targeting Players that are, fer example, in gamemode 0 or 2, use scoreboards (vis my answer):

First, create a dummy scoreboard objective:

/scoreboard objectives add gameM dummy  

Then, add all Players with gamemode Survival and Adventure to the "selectMe":

/scoreboard players set @e[m=0] gameM 1  
/scoreboard players set @e[m=2] gameM 1  

Now you can enter your command:

/testfor @a[score_gameM_min=1,r=15]  



Pictures about "How to /testfor a player's gamemode"

How to /testfor a player's gamemode - Man in Black Crew Neck T-shirt Holding Basketball
How to /testfor a player's gamemode - Man and Woman Standing on Basketball Court
How to /testfor a player's gamemode - Two People Playing Basketball on Basketball Court



How do you test if a player is sneaking in Minecraft?

The /testfor command became obsolete in Minecraft 1.13, and has been replaced by the /execute command*. It will do the same as typing /testfor @e {SelectedItem:{id:"<item>"}} and then putting a comparator to the command block with the command, and then running the command which you wanna run.



How to detect a player using command blocks! | /execute command tutorial for 1.16 Minecraft Java




Sources: Stack Exchange - This article follows the attribution requirements of Stack Exchange and is licensed under CC BY-SA 3.0.

Images: Tima Miroshnichenko, Tima Miroshnichenko, Tima Miroshnichenko, Tima Miroshnichenko