I tried to make my admin commands and I found an easy tutorial on YouTube on how to do it, I watched it and did it but the script isn’t working, Can someone assist me with this? Thanks.
Tutorial I watched: https://www.youtube.com/watch?v=xNahID7bCxA
Script I wrote:
local CommandPrefix = '!'
local Admins = {
['TrackoTheTaco'] = "Admin";
['legomonster190'] = "Admin";
}
--//Admin\\--
local function CheckForTarget(Target)
if not Target then
warn("Target not found.")
return false
else
print("Target Found")
return true
end
end
PlayersService.PlayerAdded:Connect(function(Player)
if Admins[Player.Name] then
print("An Administrator has joined the game. ")
Player.Chatted:Connect(function(Msg)
if Msg:sub(1,6) == "kill " or Msg:sub(1,6) == "Kill " then
local Target = Players:FindFirstChild(Msg:sub(7))
local Valid = CheckForTarget(Target)
if Valid then
Target.Character.Head:Destroy()
end
else
warn("Invalid Command.")
end
end
end
end
end)
local TargetName = Msg:split(’ ')
local Target = Players:FindFirstChild(TargetName[2])
string.split() or Message:Split(x) returns a table with the message separtated with the x
So Msg:split(’ ') is splitting the spaces!
Also, you shouldn’t make a function to detect the target, just do this:
PlayersService.PlayerAdded:Connect(function(Player)
if Admins[Player.Name] then
print("An Administrator has joined the game. ")
Player.Chatted:Connect(function(Msg)
if Msg:sub(1,6) == "kill " or Msg:sub(1,6) == "Kill " then
local TargetName = Msg:split(' ')
local Target = Players:FindFirstChild(TargetName[2]) -- Attempts to find the TargetName, if nil returns nil
if Target then -- Makes sure the Target is there to prevent errors
Target.Character.Head:Destroy()
end
else
warn("Invalid Command.")
end
end
end
end
end)
PlayersService.PlayerAdded:Connect(function(Player)
if Admins[Player.Name] then
print("An Administrator has joined the game. ")
Player.Chatted:Connect(function(Msg)
if Msg:sub(1,6) == "kill " or Msg:sub(1,6) == "Kill " then
local TargetName = Msg:split(' ')
local Target = Players:FindFirstChild(TargetName[2]) -- Attempts to find the TargetName, if nil returns nil
if Target then -- Makes sure the Target is there to prevent errors
Target.Character.Head:Destroy()
end
else
warn("Invalid Command.")
end
end
end
end
end)
local CommandPrefix = '!'
local Admins = {
['TrackoTheTaco'] = "Admin";
['legomonster190'] = "Admin";
}
--//Admin\\--
local function CheckForTarget(Target)
if not Target then
warn("Target not found.")
return false
else
print("Target Found")
return true
end
end
PlayersService.PlayerAdded:Connect(function(Player)
if Admins[Player.Name] then
print("An Administrator has joined the game. ")
Player.Chatted:Connect(function(Msg)
if Msg:sub(1,6) == "kill " or Msg:sub(1,6) == "Kill " then
local Target = Players:FindFirstChild(Msg:sub(7))
local Valid = CheckForTarget(Target)
if Valid then
Target.Character.Head:Destroy()
end
else
warn("Invalid Command.")
end
end
end
end
end)
There’s a couple of things I see when taking a look at your script. One of them is your prefix, and the other being where it gets the player. Allow me to explain what I mean.
The Prefix
It seems you have a prefix variable, but it’s unused in the command script, thus if you were to try !kill plr it will not work. The solution to this’s to substring starting from after the prefix (prefix length + 1).
local Prefix = "!";
if (Message:sub(1, #Prefix) == Prefix) then
Message = Message:sub(1 + #Prefix);
end
Getting the Player
For your get player method, it’s fairly simple; however, the downside’s that it’s case sensitive, meaning that you’ll have to add in any capitalization and enter the full name. The solution to this’s to write a custom get player method to get the player.
local function GetPlayer(Message)
Message = Message:lower(); -- Lowercase what the player said
for _, Player in ipairs(game.Players:GetPlayers()) do -- Iterate through the players
if (Player.Name:sub(1, #Message):lower() == Message) then -- Compare the name via a substring, and lowercase the player's name
return Player; -- Return the player if a match was found
end
end
return nil; -- Return nil if no player was found
end
If you have any questions, please let me know. I hope this helped.
EDIT
Something I wanted to point out real quick, kill is 5 characters long, but your substring’s 6 characters long. Might want to change it to Msg:sub(1, 5):lower() == "kill ".
Hello, Thanks for your reply! I just want to know how to organize it, Sorry, I only have 4-5 months of scripting experience and I am not at that level yet.
Is this how you do it?
local Prefix = "!";
local function GetPlayer(Message)
if (Message:sub(1, #Prefix) == Prefix) then
Message = Message:sub(1 + #Prefix);
end
Message = Message:lower(); -- Lowercase what the player said
for _, Player in ipairs(game.Players:GetPlayers()) do -- Iterate through the players
if (Player.Name:sub(1, #Message):lower() == Message) then -- Compare the name via a substring, and lowercase the player's name
return Player; -- Return the player if a match was found
end
end
return nil; -- Return nil if no player was found
end
You almost got it; this’s how it would look like in action.
-- Assuming the Prefix variable and GetPlayer function was defined prior
if (Message:sub(1, #Prefix) == Prefix) then
Message = Message:sub(1 + #Prefix); -- Get the message after the prefix
if (Message:sub(1, 5):lower() == "kill ") then -- Check if a player chatted "kill "
local Target = GetPlayer(Message:sub(6)); -- Attempt to get the player
if (Target ~= nil) then -- Do we have the player?
Target.Character.Humanoid.Health = 0; -- Kill the player
end
end
end
No, C++ is not used for web development. The engine is the backend part of Roblox games like how Lua functions are implemented, basic physics, networking, etc.