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)
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.