Hello. I have this car-spawning script. I want to convert this script to using the TextChatCommand instance, but I got confused on what to change.
Script:
task.wait()
local ServerStorage = game:GetService("ServerStorage")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Command = "!spawncar"
local Main = {
["cars"] = {
["jeep_old"] = ServerStorage:WaitForChild("vehicles"):FindFirstChild("Jeep"), -- there is more cars, i only put 1 to cleanup script
};
["Whitelisted_UserId"] = {
205113231
}
}
local function GetCharacter(plr : Instance)
local character = plr.Character
if character then
return character
end
end
game.Players.PlayerAdded:Connect(function(plr)
for _, g in pairs(Main.Whitelisted_UserId) do
if plr.UserId == g then
plr.Chatted:Connect(function(msg)
local args = string.split(msg, " ")
if string.lower(args[1]) == Command then
for n, v in next, Main.cars do
if string.lower(args[2]) == n then
local Car = v:Clone()
if Car then
local Character = GetCharacter(plr)
Car.Name = plr.Name .. n
Car.Parent = game.Workspace
if Character then
if Character:FindFirstChild("HumanoidRootPart") then
Car:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.Position + Vector3.new(0, 0, -12)))
end
end
end
end
end
end
end)
else
game.ReplicatedStorage.Error401:FireClient()
end
end
end)
local Command = "!spawncar"
local TextChatCommandFolder = game.TextChatService:WaitForChild("TextChatCommands")
local Command = Instance.new("TextChatCommand", TextChatCommandFolder)
Command.Name = Command
Command.PrimaryAlias = Command
Command.Enabled = true
Command.Triggered:Connect(function(plrtextsource, msg)
local plr = game.Players:FindFirstChild(plrtextsource.Name)
local args = string.split(msg, " ")
for _, g in pairs(Main.Whitelisted_UserId) do
if plr.UserId == g then
--paste the rest of the script in here
end
end
end)
you might have to change your command from !spawncar to /spawncar
The part where I pasted the rest of the code made the script stop working.
It didn’t yield any errors too.
Command.Triggered:Connect(function(plrtextsource, msg)
local plr = game.Players:FindFirstChild(plrtextsource.Name)
local args = string.split(msg, " ")
for _, g in pairs(Main.Whitelisted_UserId) do
if plr.UserId == g then
if string.lower(args[1]) == Command then
for n, v in next, Main.cars do
if string.lower(args[2]) == n then
local Car = v:Clone()
if Car then
local Character = GetCharacter(plr)
Car.Name = plr.Name .. n
Car.Parent = game.Workspace
if Character then
if Character:FindFirstChild("HumanoidRootPart") then
Car:SetPrimaryPartCFrame(CFrame.new(Character.HumanoidRootPart.Position + Vector3.new(0, 0, -12)))
end
end
end
end
end
end
else
game.ReplicatedStorage.Error401:FireClient()
end
end
end)
Here’s a heavily modified snippet from my personal admin system to quickly make TextChatCommands
function CreateTextChatCommand(CommandName: string, Callback: (TextSource: TextSource, String: string, self: {any}) -> (any), PrimaryAlias: string, SecondaryAlias: string?): TextChatCommand
local self = {}
assert(typeof(CommandName) == 'string', "Expected string, got '"..typeof(CommandName).."' for CommandName.")
assert(typeof(PrimaryAlias) == 'string', "Expected string, got '"..typeof(PrimaryAlias).."' for PrimaryAlias.")
self.CommandName = CommandName
self.Callback = Callback
self.PrimaryAlias = CommandPrefix..PrimaryAlias
if SecondaryAlias then
self.SecondaryAlias = CommandPrefix..SecondaryAlias
end
self.TextChatCommand = Instance.new("TextChatCommand", DefaultCommandStore)
self.TextChatCommand.Name = self.CommandName
self.TextChatCommand.PrimaryAlias = self.PrimaryAlias
if self.SecondaryAlias then
self.TextChatCommand.SecondaryAlias = self.SecondaryAlias
end
self.TextChatCommand.Triggered:Connect(function (TextSource, String)
local s, e = pcall(function ()
Callback(TextSource, String, self)
end)
if e then
warn("Error with callback: "..e)
end
end)
return self
end
I had a bunch of things to check whether or not you are authorized to use that command but you can just put whatever function you want to run as an arugment and it should just work
function MyFunction(TextSource, MessageString, self)
print("hi")
end
-- you would run this command by saying [CommandPrefix].."Run" in chat. The secondary alias is not necessary
CreateTextChatCommand("CommandName", MyFunction, "Run", "Run2")