Get PartialName in a command

Hey there, I am making my own admin commands and I want to be able to find the player from their partial name instead of typing a player’s full username.

--// FUNCTIONS

local function PartialName(PartialName)
	local FoundPlayer = nil
	local Players = game.Players:GetPlayers()

	for i = 1, #Players do
		local CurrentPlayer = Players[i]

		if string.find(string.lower(CurrentPlayer.Name), string.lower(PartialName)) then
			FoundPlayer = CurrentPlayer.Name
		end
	end

	if not FoundPlayer then
		return nil	
	else
		return FoundPlayer
	end
end


--// Speed Command
Commands.speed = function(player, args)
	if args[1] == "me" then
		if tonumber(args[2]) then
			player.Character.Humanoid.WalkSpeed = args[2]
		end
	else
		for i, v in pairs(Players:GetPlayers()) do
			if string.lower(PartialName(v.Name)) == args[1] then
				local PlayerToSpeed = v
				print(v)
				
				PlayerToSpeed.Character.Humanoid.WalkSpeed = args[2]
			end
		end
	end
end

I have gave this a go, however it didn’t work and I don’t know how I could do this. Any help will be appreciated.

Something like this?

function getPlr(msg:string)
	local p

	for _, player in game.Players:GetPlayers() do
		if player.Name:match(msg) then
			p = player
			break
		end
	end

	return p
end

Why do you need to check if it is nil? You could just return FoundPlayer, you don’t even need to check if its nil.

1 Like

This should work to how he’d like with a few changs,

function getPlr(msg:string)
	local p

	for _, player in game.Players:GetPlayers() do
		if player.Name:lower():find(msg:lower()) then
			p = player
			break
		end
	end

	return p
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.