✓ Accepted answer
Typically what I will do for an admin command script is make my own function to iterate through all of the players and try to match the name. I would not recommend using string.match() in this instance as you may get a bad comparison. Say you were searching for "AGuy" and there are two people "AGuyWithAShortName" and "IAmAGuy". You might actually end up hitting IAmAGuy unintentionally if he were to have joined the server first.
The basic idea for the code is to get the string that the admin has said and compare it to the other player's names. However, the for other player's name, you will need to compare partial names. So say I had "The" and I am comparing it to "TheLastTheUltimate", I would have to make "TheLastTheUltimate" a partial name. When I compare the string the admin provided and the substring from the username, they should end up matching.
function GetPlayer(targetedPlayerName)
for _,player in pairs(game:GetService("Players"):GetPlayers()) do
if targetedPlayerName:lower() == player.Name:sub(1,targetedPlayerName:len()):lower() then
return player
end
end
return nil
end
Now in your code, you would be able to get a player from a command such as ":kill M39a" such as below.
if command:sub(1,6) == ":kill " then
local target = GetPlayer(command:sub(7))
if target then
target.Character:BreakJoints()
end
end
######Hopefully this answered your question. If it did, do not forget to hit the accept answer button. If you have any questions, feel free to comment down below and I will get back to you.
1 comment
That seems to be over-complicating things: you could use the 'find' function, which would produce a simpler result. (string:find(thing) == 1)