Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

String.sub / String manipulation for this admin script won't work?

I am attempting to make my own admin script. I have a command set up to kill a specified player, which works and all, but I have also set it up so if the second word is equal to 'me' then it will kill the player who sent the message.

It works if I put another player's name, but not if I put 'me', I believe it is something to do with the string.sub, as I don't really know how to use it properly. I have no errors in the output. Any help is appreciated, thanks.

local Admins = {
	["19498972"] = true,	
}

local prefix = ";" 
local commandTable = {}
 
function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function commandTable.kill(message, player)
	if string.sub(message, 7, 8) == "me" then
		player.Character:BreakJoints()
	else
   		victim = findPlayer(message)
   			if victim and victim.Character then
       			victim.Character:BreakJoints()
		end
	end
end

game.Players.PlayerAdded:Connect(function(player)
    if Admins[tostring(player.UserId)] then
        player.Chatted:Connect(function(message)
            local command, argument = message:match(prefix.."(.+) (.+)")
            if command and commandTable[command] then
                commandTable[command](argument, player)
            end
        end)
    end
end)

The error, I believe is on line 17.

4 comments

0

PM me on ROBLOX, I can just give you an admin script of mine. It has a lot of fancy features, and it doesn't require you to count string.sub and stuff.

0

Make sure you message me on the account "D3D9", it's my new main. Follow to message me.

0

No thank you. The reason I am here is to fix my bugs, so I can learn about it. I don't want any free scripts or models. It defeats the point of my question and this websites purpose.

0

Posting a reply any moment, testing out the Script first

1 answer

✓ Accepted answer

So i just looked into your script and saw that you are putting the command and arguments in 2 Values, command and argument, then you're checking from the start of position 7 if the string is me, which is not.

In short: You gave the Script an wrong start position to check for your "target".

If you would use if string.sub(message, 1, 2) == "me" then then it would recognize that you are selecting yourself.

Here is the full code, and sorry if you didn't understand a single bit, first time helping someone

local Admins = {
    ["19498972"] = true, 
}

local prefix = ";" 
local commandTable = {}

function findPlayer(name)
    for _, player in ipairs(game.Players:GetPlayers()) do
        if player.Name:lower() == name:lower() then
            return player
        end
    end
end

function commandTable.kill(message, player)
    if string.sub(message, 1, 2) == "me" then
        player.Character:BreakJoints()
    else
        local victim = findPlayer(message)
            if victim and victim.Character then
                victim.Character:BreakJoints()
        end
    end
end

game.Players.PlayerAdded:Connect(function(player)
    if Admins[tostring(player.UserId)] then
        player.Chatted:Connect(function(message)
            local command, argument = message:match(prefix.."(.+) (.+)")
            if command and commandTable[command] then
                commandTable[command](argument, player)
            end
        end)
    end
end)

This discussion is preserved as it was. New answers, comments, and votes are closed.