'ROBLOX Kill command to kill specified player

I want to make a command that would kill a player you specify.. Let's say I type: kill/Paul. Now I want to kill the player with the name Paul.

This is my command Script:

local player = ...
    
game.Players.PlayerAdded:connect(function(player) --this gets the player that connected
    player.Chatted:connect(function(message) --this function executes when the player type into chat
    --commands are here
        if player.Name == "TominoCZ" or player.Name == "nathancain" or player.Name == "block100000" then
            if message == "kill/me" then
                player.Character.Head:remove()
            end
        
            if message == "ff/me" then
                if player.Character:findFirstChild("ForceField") then
                    player.Character.ForceField:Destroy()
                end
            
                Instance.new("ForceField").Parent = player.Character
            end
            
            if message == "unff/me" then 
                if player.Character:findFirstChild("ForceField") then
                    player.Character.ForceField:Destroy()
                end
            end
        end
    end)
end)

Now you can see that I already have a command that will kill the play that executed it.

But how can I kill a different player by specifying the player's name after the "kill/"?

This command script might look too long or not too pro, but atleast I know and understand what it does.

So any ideas?



Solution 1:[1]

You can do something like this:

local player = ...

game.Players.PlayerAdded:connect(function(player) --this gets the player that connected
    player.Chatted:connect(function(message) --this function executes when the player type into chat
    --commands are here
    if string.sub(message,1,string.len("kill/"))=="kill/" then --check the message if it starts with command "kill/"
        if string.sub(message,string.len("kill/"))==player.Name then --kill the player with the name
            player.Character.Head:remove()
            end
    end)
end)

I don't know Lua so there might be syntax errors, but the overall idea is that you use string.sub method to divide your message into 2 parts: the command part and the info part. If the command part equals to "kill/", then find the player with the name specified in the info part, and kill him! (Or behead him... I don't play ROBLOX :D)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 NoImNotNull