Help with admin console line

Im trying to make a admin command console line:


https://gyazo.com/a0005282846a7dce0d18991b5e9e7639

I need help with the string.split so like
When you type in “ws me 50” it changes your walkspeed to “50”
and so on and doing:

if cmd.Text == "ws me 500" then
char.Humanoid.WalkSpeed = 50
end

is not good because it is only for one speed amount how do I do this?

Hey I think this is what you’re trying to achieve:

-- LOCAL SCRIPT

--// Services

local replicatedStorage = game:GetService("ReplicatedStorage")

--// Variables

local player = game:GetService("Players").LocalPlayer

local commandEvent = replicatedStorage:WaitForChild("CommandEvent")

--// Main Code

local cmdBox = script.Parent:WaitForChild("CmdBox")

cmdBox.FocusLost:Connect(function()

local textFormat = string.split(cmdBox.Text, " ")

commandEvent:FireServer(textFormat)

end)
-- SERVER SCRIPT

--// Services

local replicatedStorage = game:GetService("ReplicatedStorage")

--// Variables

local remoteEvent = Instance.new("RemoteEvent")
remoteEvent.Name = "CommandEvent"
remoteEvent.Parent = replicatedStorage

--// Main Code

remoteEvent.OnServerEvent:Connect(function(player, textFormat)
	print(textFormat[1], textFormat[2], textFormat[3])
	if textFormat[1] == ":ws" or textFormat[1] == ":walkspeed" then
		if textFormat[2] == "me" and tonumber(textFormat[3]) then
			player.Character.Humanoid.WalkSpeed = tonumber(textFormat[3])
		end
	end
end)

Command System.rbxl (18.3 KB)

4 Likes

I’d suggest learning Lua string patterns. They’re very powerful and allow you to accomplish things like this in a heartbeat.
To answer your immediate question, you could use string.split, string.find, string.lower, string.gsub and ModuleScripts (for cleanliness).

local prefix = ":" -- command prefix

game.Players.PlayerAdded:Connect(function(plr)
    plr.Chatted:Connect(function(msg)
        if msg:find("^"..prefix) then
            local args = string.split(msg:gsub("^"..prefix, ""), " ") -- split by spaces
            if script:FindFirstChild(args[1]:lower()) then
                require(script[args[1]:lower()])(plr, args)
                -- this requires a module with the same name as the command,
                -- then calls the function it returns with 'plr' and 'args'
            else
                print("Command not found.")
            end
        end
    end)
end)

And finally:

-- module named "ws" parented to the last script

return function(plr, args)
    if args[2]:lower() == "me" and plr.Character then
        -- ws me
        plr.Character.Humanoid.WalkSpeed = tonumber(args[3])

    elseif args[2]:lower() == "others" then
        -- ws others
        for i,v in pairs(game.Players:GetPlayers()) do
            if v ~= plr and v.Character then
                v.Character.Humanoid.WalkSpeed = tonumber(args[3])
            end
        end

    else
        -- ws player_name
        for i, v in pairs(game.Players:GetPlayers()) do
            if v.Name:lower():find("^"..args[2]:lower()) and v.Character then
                v.Character.Humanoid.WalkSpeed = tonumber(args[3])
                break
            end
        end
    end
end

To add another command, it’s as simple as adding another module with the name of your command, like jp or ff, or even commandthatnobodywillbothertotype!

And, yes, my code looks like a mess. My code is always a mess, just like my life.

Hope I helped! :wink:

6 Likes

This is most probably the most efficient way of scripting the admin system, my example simply went off the OP’s main layout.