Admin Command Script not working

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)

Uh, the tutorial linked is to make a cup?

1 Like

Hello!

You should use:

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)
2 Likes

Sorry about that, Wrong link, Fixed!

Do I replace the

local CommandPrefix = '!'
local Admins = {

['TrackoTheTaco'] = "Admin";
['legomonster190'] = "Admin";
}

with

local TargetName = Msg:split(’ ')
local Target = Players:FindFirstChild(TargetName[2])

?

Just replace 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)

From Player added to the end as I showed

It doesn’t work and the output says: [ Expected ‘)’ (to close ‘(’ at line 48), got ‘end’]

Nevermind…
Try the old code

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)

JUST define PlayersService

local PlayersService = game.Players

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. :slight_smile:

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

1 Like

I think the trouble was just he didn’t defined PlayersService

1 Like

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

Lemme know if this helped at all. :slight_smile:

1 Like

Hello! The reason why this code does not work is because it is out of date, roblox update LUA so the version of LUA you are using is out-of date

1 Like

I feel like we are almost there, This is what the output says: ServerScriptService.Script:14: Expected ‘end’ (to close ‘function’ at line 2), got

The video was made in 2018 so I guess LUA changed.

Lua* (name, not acronym) code cannot be “out of date”. Roblox always has used Lua 5.1.

1 Like

Mind showing your script with the edits done to it?

Small Edit
Please lets not stray too off topic.

I think it does update sometimes, But it’s rare though.

Oh, So the website is made with C++ and the roblox studio code language is LUA?

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.

4 Likes