Custom Player Commands

This tutorial will teach you how to create custom player commands.

by Danilka7575

Author Avatar

Custom Player Commands

Hello. In this tutorial I will teach you how to make simple custom player commands using string.sub.

Let's get started

First, insert a script into ServerScriptService. Name it however you want. Let's start from simple.

Write this in your script:

game.Players.PlayerAdded:Connect(function(Player) -- Player added event
    Player.Chatted:Connect(function(Message) -- Player chatted event
        -- Lowering message and checking that message if it is ":heal"
        if Message:lower() == ":heal" then
            -- Setting character's health to max health
            Player.Character.Humanoid.Health = Player.Character.Humanoid.MaxHealth
        end
    end
end

As we can see, we created simple custom command which heals you. As soon as we type ":heal", it will set character's health to max.

Let's add more commands:

game.Players.PlayerAdded:Connect(function(Player) -- Player added event
    Player.Chatted:Connect(function(Message) -- Player chatted event
        -- Lowering message and checking that message if it is ":heal"
        if Message:lower() == ":heal" then
            -- Setting character's health to max health
            Player.Character.Humanoid.Health = Player.Character.Humanoid.MaxHealth
        elseif Message:lower():sub(1, 6) == ":kick " then
            local TargetPlayer = Message:sub(7)
            -- If player is found, it will kick him
            if game.Players:FindFirstChild(TargetPlayer) then
                -- Prints that player is found and kicks him
                print("Player found.")
                game.Players[TargetPlayer]:Kick("You are was kicked by "..Player.Name..".")
            else
                -- Prints that player not found
                print("Player not found.")    
            end
        end
    end
end

As you can see, I used sub(). It's like returns substring where a (sub(a, b) for example) is start line in string and b is end line.

local String = "Hello, world!"
print(String:sub(1, 5)) -- output: hello

Let's add command which will be allowed for players who bought gamepass.

local MarketplaceService = game:GetService("MarketplaceService") -- Getting MarketplaceService
local GamepassID = 0 -- Put your gamepass ID here

game.Players.PlayerAdded:Connect(function(Player) -- Player added event
    Player.Chatted:Connect(function(Message) -- Player chatted event
        -- Lowering message and checking that message if it is ":heal"
        if Message:lower() == ":heal" then
            -- Setting character's health to max health
            Player.Character.Humanoid.Health = Player.Character.Humanoid.MaxHealth
        elseif Message:lower():sub(1, 6) == ":kick " then
            local TargetPlayer = Message:sub(7)
            -- If player is found, it will kick him
            if game.Players:FindFirstChild(TargetPlayer) then
                -- Prints that player is found and kicks him
                print("Player found.")
                game.Players[TargetPlayer]:Kick("You are was kicked by "..Player.Name..".")
            else
                -- Prints that player not found
                print("Player not found.")    
            end
        elseif Message:lower() == ":fire" then
            -- Checks if player owns gamepass
            if MarketplaceService:UserOwnsGamePassAsync(Player.UserId, GamepassID) then
                -- Prints that player own gamepass and sets fire to character's head
                print("Player own gamepass.")
                local Fire = Instance.new("Fire", Player.Character.Head)
            else
                -- Prints that player don't own gamepass
                print("Player don't own gamepass.")
            end
        end
    end
end

This is the end. I hope this helped you.

View in-game to comment, award, and more!