in-game command maker

  • Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

  • LOOKING FOR A VERSION OF SKRIPT?

    You can always check out skUnity Downloads for downloads and any other information about Skript!

etheboi_

Active Member
Nov 9, 2023
87
1
8
23
So I'm trying to make a command where you can make little commands like -hello or -diamond. And I have a good idea for it.

My code is:

command /command [<text>] [<text>] [<text>]:
permission: command.maker
permission message: "&4You can not run this command!"
trigger:
if arg-1 is "create":
set {_command} to "-%arg-2%"
set {_command.%arg-2%.output} to arg-3
if "%{command.list::*}%" contains "%{_command}%%nl%":
send "&4That command already exists!" to player
else:
add "%{_command}%%nl%" to {command.list::*}
send "&aCreated command -%{_command}%" to player
if arg-1 is "list":
send "&b%{_command.list::*}%" to player
if arg-1 is "clear":
set {command.list::*} to ""
send "&bReset all commands" to player



on chat:
cancel event
if "%{command.list::*}%" contains "-%{_command}%%nl%":
set {_message} to last (length of {_message} - 1) characters of message
make server execute command "/function %{_command.%{_message}%.output}%"
else:
loop all players:
send "<%player%> %message%" to loop-players


It is registering commands but not executing them. Please help
 
Better code:
Code:
command /command [<text>] [<text>] [<text>]:
    permission: command.maker
    permission message: "&4You can not run this command!"
    trigger:
        if arg-1 is "create":
            set {_command} to "-%arg-2%"
            set {_command.%arg-2%.output} to arg-3
            if "%{command.list::*}%" contains "%{_command}%%nl%":
                send "&4That command already exists!" to player
            else:
                add "%{_command}%%nl%" to {command.list::*}
                send "&aCreated command -%{_command}%" to player
        if arg-1 is "list":
            send "&b%{_command.list::*}%" to player
        if arg-1 is "clear":
            set {command.list::*} to ""
            send "&bReset all commands" to player



on chat:
    cancel event
    if "%{command.list::*}%" contains "-%{_command}%%nl%":
        set {_message} to last (length of {_message} - 1) characters of message
        make server execute command "/function %{_command.%{_message}%.output}%"
    else:
        loop all players:
            send "<%player%> %message%" to loop-players
 
Right off the bat, I can see that the type of variable you’re using for the type of command is likely not one you’ll want to use. {_command} will only be saved in the lines of code it’s used. So if you were to send “%{_command}%” on the On Chat event, it will print <none> since it won’t be set. To fix this, remove the underscore and attach %player% or %player’s uuid% to it like so {command.%player%}. This is so that one, it can be used outside the event that variable is set and two, so that multiple players don’t have access to the same commands (An addition I think that’d be helpful). Below is the modified code with these fixes (Not tested in game):

Code:
command /command [<text>] [<text>] [<text>]:
    permission: command.maker
    permission message: "&4You can not run this command!"
    trigger:
        if arg-1 is "create":
            set {command.%player%} to "-%arg-2%" # Attaching the player to the variable also prevents {command} from being changed by another player
            set {command.%player%.%arg-2%.output} to arg-3
            if "%{command.list::*}%" contains "%{command.%player%}%%nl%":
                send "&4That command already exists!" to player
            else:
                add "%{command.%player%}%%nl%" to {command.list::*}
                send "&aCreated command -%{command.%player%}%" to player
        if arg-1 is "list":
            send "&b%{command.list::*}%" to player # Removed underscore from this variable to keep it consistent
        if arg-1 is "clear":
            clear {command.list::*} # Changed set to clear for better optimization
            send "&bReset all commands" to player



on chat:
    cancel event
    if "%{command.list::*}%" contains "-%{command.%player%}%%nl%":
        set {_msg} to last (length of {_msg} - 1) characters of message
        make server execute command "/function %{command.%player%.%{_msg}%.output}%" # Reusing the same variable defined before
    else:
         broadcast "<%player%> %message%" # Instead of looping all players, you can simply use a single line broadcast that sends a message to all players without a loop
Changes I made: Made a couple of the lines more effective, shortened the name of a variable and changed your variable types to save data better.
 
  • Like
Reactions: Merrical
Right off the bat, I can see that the type of variable you’re using for the type of command is likely not one you’ll want to use. {_command} will only be saved in the lines of code it’s used. So if you were to send “%{_command}%” on the On Chat event, it will print <none> since it won’t be set. To fix this, remove the underscore and attach %player% or %player’s uuid% to it like so {command.%player%}. This is so that one, it can be used outside the event that variable is set and two, so that multiple players don’t have access to the same commands (An addition I think that’d be helpful). Below is the modified code with these fixes (Not tested in game):

Code:
command /command [<text>] [<text>] [<text>]:
    permission: command.maker
    permission message: "&4You can not run this command!"
    trigger:
        if arg-1 is "create":
            set {command.%player%} to "-%arg-2%" # Attaching the player to the variable also prevents {command} from being changed by another player
            set {command.%player%.%arg-2%.output} to arg-3
            if "%{command.list::*}%" contains "%{command.%player%}%%nl%":
                send "&4That command already exists!" to player
            else:
                add "%{command.%player%}%%nl%" to {command.list::*}
                send "&aCreated command -%{command.%player%}%" to player
        if arg-1 is "list":
            send "&b%{command.list::*}%" to player # Removed underscore from this variable to keep it consistent
        if arg-1 is "clear":
            clear {command.list::*} # Changed set to clear for better optimization
            send "&bReset all commands" to player



on chat:
    cancel event
    if "%{command.list::*}%" contains "-%{command.%player%}%%nl%":
        set {_msg} to last (length of {_msg} - 1) characters of message
        make server execute command "/function %{command.%player%.%{_msg}%.output}%" # Reusing the same variable defined before
    else:
         broadcast "<%player%> %message%" # Instead of looping all players, you can simply use a single line broadcast that sends a message to all players without a loop
Changes I made: Made a couple of the lines more effective, shortened the name of a variable and changed your variable types to save data better.
Sadly, the fixed code does not work. It registers the command but does not execute the function when I run the command that I created.
Code:
/command create idk idk:unknown
OUTPUT: Created command -idk
-idk
OUTPUT: <etheboi> -idk
THANKFULLY:
/command list
OUTPUT: -idk
These are the exact commands I ran, and it can detect if a command is already been made.
 
Code:
command /command [<text>] [<text>] [<text>]:
    permission: command.maker
    permission message: "&4You cannot run this command!"
    trigger:
        if arg-1 is "create":
            add {command.%player%} to {} # Initialize command list if not already
            set {command.%player%.%arg-2%.output} to arg-3
            if "%{command.list::*}%" contains "-%{command.%player%}%%nl%":
                send "&4That command already exists!" to player
            else:
                add "-%{command.%player%}%" to {command.list::*}
                send "&aCreated command -%{command.%player%}%" to player
        if arg-1 is "list":
            send "&b%{command.list::*}%" to player
        if arg-1 is "clear":
            clear {command.list::*}
            send "&bReset all commands" to player

on chat:
    cancel event
    set {_msg} to last (length of message - 1) characters of message
    if "%{command.list::*}%" contains "-%{_msg}%%nl%":
        make server execute command "/function %{command.%player%.%{_msg}%.output}%"
    else:
        broadcast "<%player%> %message%"

works for me
 
On line 6 (add {command.%player%} to {} # Initialize command list if not already) there's an error, what was the code that was put in the {} bit here? I tried deleting the line and it still didn't work, it assigned the command but there was no output when I typed in chat -idk. Just to clarify, the 3rd argument is for a function from a datapack that is ran when the assigned command is executed.