Getting a Player from a string shortcut

I wrote this simple kick script:

game.Players.PlayerAdded:connect(function(plr)
	plr.Chatted:connect(function(msg)
		local args = msg:split(" ")
		if args[1] == "!kick" then
			if plr:GetRankInGroup(5555641)>= 199 then
			local target = game.Players:FindFirstChild(args[2])
			target:Kick("You have been kicked by a staff member")
			end
		end
		
		
	end)
end)

I want to add to it so that I don’t have to type the user’s full username including caps to get the player. I don’t know how to do this.
For example: !kick Aqveria → !kick Aqv
Any help is appreciated.

1 Like

you can use:

for _,Plr in ipairs(game.Players:GetChildren()) do
       if string.sub(Plr.Name,1,#args[2]) == args[2] then
             Plr:Kick("You have been kicked by a staff member"))
       end
end
1 Like

Yes, just go through all the players and check if the partial is contained in their name.

local function get_player_from_partial(s)
    s = s:lower() -- Remove this if you want case sensitivity
    for _, player in ipairs(Players:GetPlayers()) do
        if s == player.Name:lower():sub(1, #s) then
            return player
        end
    end
    return nil
end

And as a side note, :connect is deprecated in favour of :Connect

1 Like

The issue with that is, it doesn’t only search from the beginning. It can allow in the middle which can cause problems.

1 Like

This is what I did, and there is no errors but it isn’t working.

game.Players.PlayerAdded:connect(function(plr)
	plr.Chatted:connect(function(msg)
		local args = msg:split(" ")
		if args[1] == "!kick" then
			if plr:GetRankInGroup(5555641)>= 199 then
			local function get_player_from_partial(s)
    s = s:lower() 
    for _, player in ipairs(game.Players:GetPlayers()) do
        if s == player.Name:lower():sub(1, #s) then
            return player:Kick("Kicked.")
        end
    end
    return nil
end
			end
		end
		
		
	end)
end)

Thanks.

This works, it is just case sensitive. Thank you for your help tho, it pointed me in the right direction.

You need to call the function, and you should define it at the top for reusability otherwise you’re defeating the purpose of functions.

local Players = game:GetService("Players")

local function get_player_from_partial(s)
    s = s:lower() -- Remove this if you want case sensitivity
    for _, player in ipairs(Players:GetPlayers()) do
        if s == player.Name:lower():sub(1, #s) then
            return player
        end
    end
    return nil
end

Players.PlayerAdded:Connect(function(player)
    player.Chatted:Connect(function(msg)
        local args = msg:split(" ")

        if args[1] == "!kick" then
            local target = get_player_from_partial(args[2])
            
            if target then
                target:Kick("You were kicked by a staff member")
            end
        end
    end)
end)

Thank’s for the help. I’m not used to using functions like that. I’m honestly not good at scripting at all. UI is what I’m best at. Thanks for the help.

It shouldn’t be case sensitive since you have:

If what you meant is you want it to be case sensitive, then remove that line.

Edit:

If you mean the “!kick” part is case sensitive, then do:

if args[1]:lower() == "!kick" then

instead of