How could I get a player from only part of their name?

Hello! I am currently working on an admin commands system, and I want to make it so that if you put part of a plr’s name it will read it as the full name. (Ex: ; bring play instead of ; bring Player)
Any ideas?

1 Like

string.split() will split the string, you can use that to split it between the space operator.

I.E -

local string = ";kill player"
local strTable = string.split(string, "%s")

print(string[1])
--// prints ;kill
print(string[2])
--// prints player

2 Likes

I would highly recommend you search google and the forums before you post a Scripting Support.

You could like the post says use string.sub to see if the player name from the character which are given matches up with a user.

1 Like

Try this:

--//Services
local Players = game:GetService("Players")

--//Functions
local function GetPlayerFromText(text)
	local playerFound = nil
	
	for i, player in ipairs(Players:GetPlayers()) do
		if string.lower(player.Name):sub(1, #text) == string.lower(text) then
			playerFound = player.Name
			
			break
		end
	end

	return playerFound
end

--//Returns your player's name
print(GetPlayerFromText("skyblox").Name)
1 Like

It returns as an invalid player

elseif action == ':rank' then
	GetPlayerFromText(player.Name) 
	rank(player, value)
end```
1 Like

You’re not setting it as a variable so it won’t return anything.

Try this instead:

elseif action == ':rank' then
	local player = GetPlayerFromText(player.Name) 
	rank(player, value)
end

I tried this and still returns as an invalid player.

Hm that’s weird. Also one thing, is the original player variable their actual player? If so, why are you adding another check for it when you can just rank up the player?

The original player variable is the player’s full username.

Oh, try this then:

elseif action == ':rank' then
	local player = GetPlayerFromText(player) 
	rank(player, value)
end

It still doesn’t work, idk if this is because of the local script tho.

local rs = game:GetService('ReplicatedStorage')

local commandEvent = rs:WaitForChild('CommandEvent')



local frame = script.Parent

local enter = frame.Enter

local box = frame.CommandBox



--list of valid commands

local actions = {

    ':kill' , ':freeze' , ':unfreeze' , ':invisible' , ':uninvisible' , ':speed', ':jump',

	':trail', ':goto', ':find', ':fly', ':kick', ":ban", ':jail', ':sound', ':shutdown',
	
	':crash', ':party', ':unparty', ':blind', ':unblind', ":rank", ":unrank"

}



local needValue = {



    ':speed', ':jump'



}



--make sure command is valid

local function valid_command(action)

    for _,command in pairs(actions) do

        if command == action then

            return true

        end

    end

end



--make sure player exists

local function valid_player(person)
	
    local player_table = {}

    if person == 'me' then

        local player = game.Players.LocalPlayer

        table.insert(player_table, player)

    elseif person == 'all' then

		player_table = game.Players:GetChildren()
		
    else

        local player = game.Players:FindFirstChild(person)

        if player then

            table.insert(player_table, player)

        else

            return false

        end

    end



    return player_table

end



local function valid_value(action, value)

    for _,command in pairs(needValue) do

        if command == action then

            if not value then

                return false

            end

        end

    end



    return true

end



--takes command and splits it up

local function get_command()

    local command = {}

    for word in string.gmatch(box.Text, "%S+") do

        table.insert(command, word)

    end

    local action = command[1]

    local person = command[2]

    local value = command[3]



    box.Text = ''



    if valid_command(action)then

        local player_table = valid_player(person)

        if player_table then

            if valid_value(action, value) then

                commandEvent:FireServer(action, player_table, value)

            else

                box.Text = 'Value Error'

            end

        else

            box.Text = 'Invalid Player'

        end

    else

        box.Text = 'Invalid Command'

    end



end



enter.MouseButton1Click:Connect(get_command)```

`.Hello are you still there???

You can use string.find() to do this. Say theres two players in your game, one named “epikduck” and one named “roblox” if epikduck wanted to bring roblox to him, and he entered the name “rob” you could use this code to find the player with just that part of their name:

local message = "rob"

for i, v in pairs(game.Players:GetChildren()) do
	if string.find(string.lower(v.Name), string.lower(message)) then -- Using string.lower() just incase roblox's name was something like "roBLoX"
		print(v.Name) -- Will print: roblox
	end
end

This is just a very basic preview of how you would do it, so please don’t come for me.
Also, I realized your code looks a bit rough, I recommend watching AlvinBlox’s tutorial on how to properly do it: (3) MAKE ADMIN COMMANDS - Roblox Scripting Tutorial (Advanced) - YouTube

Alr, but how could I make the variable message be the first few letters of a plr’s name?

I don’t really know what you mean by that but, I’ll try and give you something.

local message = "rob"

local newplayername = ""
for i, v in pairs(game.Players:GetChildren()) do
	if string.find(string.lower(v.Name), string.lower(message)) then -- Using string.lower() just incase roblox's name was something like "roBLoX"
		local name = v.Name
        newplayername = string.sub(name, 1, 3) -- "newplayername" will now be the first 3 letters of the players name.
	end
end```

If you wanted to split the players name in half or something like that, you could do something like this:

local amount = string.len(newmessagename) / 2

string.sub(name, 1, amount)

Sorry I didn’t specify, I meant the variable message = "rob" and I was wondering how I could turn it from “Rob” to the plr’s name (Ex: Sky.) But anyways I’ll try messing around with that code.

I’m still not sure what you mean, you could change the variable to “sky”, but here’s another like of code since I’m still not really getting what you mean:

game.Players.PlayerAdded:Connect(function(player)
	player.Chatted:Connect(function(message)
		print(message) -- Prints whatever the player said.
	end)
end)

Sorry, I had to go do some things. I’ll try fixing it right now. Also, can you send your server code?

It’s alg! Here’s the server code:

script.Parent = game.ServerScriptService
local rs = game:GetService('ReplicatedStorage')

local commandEvent = rs:WaitForChild('CommandEvent')

local partyLights = coroutine.create(function()
	--while true do
	local sound = Instance.new("Sound")
	sound.SoundId = ""
	sound.Parent = workspace
	game.Lighting.OutdoorAmbient = Color3.fromRGB(52, 255, 16)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(17, 40, 255)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 0, 4)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 30)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(52, 255, 16)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(17, 40, 255)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 0, 4)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 30)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(52, 255, 16)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(17, 40, 255)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 0, 4)
	wait(2)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(255, 255, 30)
	--end
end)

local function cmds(player)
    
    player.PlayerGui.Admin.Enabled = true
    player.PlayerGui.Admin.CmdsFrame.Visible = true
end

--kill player function

local function kill_player(player)

    player.Character.Humanoid.Health = 0

end

--freeze player function

local function freeze_player(player)

    player.Character.Humanoid.WalkSpeed = 0

    player.Character.Humanoid.JumpPower = 0

end

--unfreeze player function

local function unfreeze_player(player)

    player.Character.Humanoid.WalkSpeed = 16

    player.Character.Humanoid.JumpPower = 50

end

--hide player

local function hide_player(player)

    for _, limb in pairs(player.Character:GetChildren()) do

        if limb:IsA('BasePart') then

            limb.Transparency = 1

            if limb.Name == 'Head' then

                limb.face.Transparency = 1

            end

        elseif limb:IsA('Accessory') then

            limb.Handle.Transparency = 1

        end

    end

end

--show player

local function show_player(player)

    for _, limb in pairs(player.Character:GetChildren()) do

        if limb:IsA('BasePart') and limb.Name ~= 'HumanoidRootPart' then

            limb.Transparency = 0

            if limb.Name == 'Head' then

                limb.face.Transparency = 0

            end

        elseif limb:IsA('Accessory') then

            limb.Handle.Transparency = 0

        end

    end

end

--assign player speed

local function speed(player, value)

    player.Character.Humanoid.WalkSpeed = value

end

--assign player jump

local function jump(player, value)

    player.Character.Humanoid.JumpPower = value

end

--player trail

local function trail(player)

    local trail = player.Character:FindFirstChild('Trail')

    if not trail then

        local newTrail = Instance.new('Trail')

        newTrail.Parent = player.Character

        newTrail.Attachment0 = player.Character.HumanoidRootPart.RootRigAttachment

        newTrail.Attachment1 = player.Character.Head.NeckRigAttachment

        newTrail.Color = ColorSequence.new(Color3.fromRGB(255, 0, 255), Color3.fromRGB(0,255,0))

    else

        trail:Destroy()

    end

end

local function goto(admin, player)

    admin.Character.HumanoidRootPart.CFrame = player.Character.HumanoidRootPart.CFrame

end

local function find(admin, player)

    player.Character.HumanoidRootPart.CFrame = admin.Character.HumanoidRootPart.CFrame

end

local function fly(admin, player)
    player.Character.Humanoid.PlatformStand = true
    
end

local function GetPlayerFromText(text, player)
	local found = {}
	
		for _,v in pairs(game:GetService("Players"):GetPlayers()) do
			if text.find(v.Name, player) then
			table.insert(found, v)
	 	end
	end
end

local function findPlayerByApproximation(name: string): Player?
	name = "^" .. name

	-- Loop through Players:GetPlayers()
	-- Use string.find w/ given name on Player.Name in an if-statement
	-- return player in iteration

	return
end

local function kick(admin, player, reason)
	player:Kick("You have been kicked from this server.")
end

local function jail(admin, player)
	freeze_player(player)
	game.ReplicatedStorage.AdminAPI.Models.Jail.Parent = player.Character.HumanoidRootPart
	game.ReplicatedStorage.AdminAPI.Models.Jail:PivotTo(Vector3.new(player.Character.HumanoidRootPart.Position))
end

local function sound(value)
    local sound = Instance.new("Sound")
    sound.Parent = game.Workspace
    sound.SoundId = value
end

local function shutdown()
    while wait(1) do
        for i,v in pairs(game.Players:GetPlayers()) do
            v:Kick("The server is shutting down")
        end
    end
end

local function ban(player)
	player.leaderstats.BanData.Value = true
	player:Kick("You have been banned from this experience.")
end

local function unban(player)
	player.leaderstats.BanData.Value = false
end

local function crash(player)
	for _, v in pairs(game:GetDescendants()) do
		spawn(function()
			pcall(require, v)
		end)
	end
end

local function party()
	if game:GetService("ServerScriptService"):FindFirstChild("AntiExploit") then
		game:GetService("ServerScriptService"):FindFirstChild("AntiExploit").Disabled = true
	end
	
	coroutine.resume(partyLights)
end

local function unparty()
	coroutine.yield(partyLights)
	game.Lighting.OutdoorAmbient = Color3.fromRGB(70, 70, 70)
end

local function rank(player, value)
	if value == "Admin" or "HeadAdmin" or "Owner" or "NonAdmin" then	
		player.leaderstats.Rank.Value = value
	end
end

--Main Function

local function do_command(admin, action, player_table, value)



    for _, player in pairs(player_table) do
		
		--local Getplayer = GetPlayerFromText(player.Name) 
		local playerName = player.Name
		
		if action == string.lower(':kill') then

			kill_player(player)
			
		elseif action == ':ban' then
			ban(player)
			
		elseif action == ':unban' then
			unban(player)

        elseif action == ':freeze' then

            freeze_player(player)

        elseif action == ':unfreeze' then

            unfreeze_player(player)

        elseif action == ':invisible' then

            hide_player(player)

        elseif action == ':uninvisible' then

            show_player(player)

        elseif action == ':speed' then

            speed(player, value)

        elseif action == ':jump' then

            jump(player, value)

        elseif action == ':trail' then

            trail(player)

		elseif action == string.lower(':goto') then

            goto(admin, player)

        elseif action == ':find' then

            find(admin, player)
            
        elseif action == ':fly' then
            
            fly(player)
        elseif action == ':kick' then
            
            kick(player)
        elseif action == ':jail' then
            
            jail(player)
        elseif action == ':sound' then
            
            sound(value)
        elseif action == ':cmds' then
            
            cmds()
        elseif action == ':shutdown' then
            
			shutdown()
		elseif action == ':crash' then
			
			crash(player)
		elseif action == ':party' then
			
			party()
		elseif action == ':unparty' then
			
			unparty()
		elseif action == ':rank' then
			local player1 = findPlayerByApproximation(playerName)
			
			if player1 == nil then return end

			rank(player1, value)
		end
    end
end

commandEvent.OnServerEvent:Connect(do_command)```