Hi devs,
I need some help with my chat commands. Right now for a kick command am doing this:
game.Players.LocalPlayer.Chatted:Connect(function(realMessage)
local message = string.lower(realMessage)
if string.sub(message, 1, 6) == ":kick " then
local player = string.sub(message, 7)
if game.Players[player] then
game.ReplicatedStorage.KickPlayer:FireServer(player)
else
end
end
end)
It works fine but I want to add a reason thingy in it. I mean if I do :kick anirudh851 test then it should kick me with the reason “test”. I have already achieved the reason in a gui, I do player:Kick(reason) but here I can’t find the index where the reason starts. I know that the player name starts at 7 index as the command :kick is fixed and it doesn’t change, but player names have dynamic length, some can be small and others can be big. I just want to know the index where the reason starts. Hope you have understood what I mean, sorry my english is bad. Thanks.
The player’s name will always be lowercase in the message variable. But what if the player’s name isn’t lowercase? I recommend you use string.sub(realMessage, 7) instead.
local chosenPlayer = nil
local function getPlayer(name)
for i, v in pairs(game.Players:GetPlayers()) do
if string.sub(v.name:lower(), 1, #name) == name:lower() then
chosenPlayer = v
end
end
if not chosenPlayer then return nil end
end
game.Players.LocalPlayer.Chatted:Connect(function(realMessage)
local message = string.split(realMessage, ' ')
if message[1]:lower() == ":kick" then
local player = getPlayer(message[2])
table.remove(message, 1); table.remove(message, 2)
local result = message:concat(' ')
if not player then return end
if game.Players[player] then
game.ReplicatedStorage.KickPlayer:FireServer(player, reason)
else
end
end
end)
Instead of using string.sub to determine if you are using the kick command, you could split the message by spaces.
Also, Player.Chatted also works on the server, this would be better for security.
local Players = game:GetService("Players")
local admins = {--[[names here]])
local function onPlayerAdded(player)
local function onChatted(message)
if not table.find(admins, player.Name) then return end
local args = message:split(" ")
if args[1] == ":kick" and #args >= 2 then
local target = getPlayer(args[2])
if target then
target:Kick("You have been kicked by "..player.Name)
end
end
end
player.Chatted:Connect(onChatted)
end
Players.PlayerAdded:Connect(onPlayerAdded)
just a second, you never declared the reason var before and never set its value either. what will be the value of the reason var? sorry, am just a new scripter.
Isn’t Player.Chatted recommended in a Script? Alongside, since you’re splitting a message with " " (space), if message[1]:lower() == ":kick " then would check for the space at the end.