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

Okay, try replacing your client code with this:

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

--//Variables
local LocalPlayer = Players.LocalPlayer
local commandEvent = ReplicatedStorage:WaitForChild('CommandEvent')
local frame = script.Parent
local enter = frame.Enter
local box = frame.CommandBox

--//Tables
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'
}

--//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

local function valid_command(action)
	for i, command in ipairs(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
		table.insert(player_table, LocalPlayer)

	elseif person == 'all' then
		player_table = Players:GetPlayers()
	else
		local player = GetPlayerFromText(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 i, command in ipairs(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 not valid_command(action) then
		box.Text = 'Invalid Command'
		
		return
	end

	local player_table = valid_player(person)
	
	if not player_table then
		box.Text = 'Invalid Player'
		
		return
	end
	
	if not valid_value(action, value) then
		box.Text = 'Value Error'
		
		return
	end

	commandEvent:FireServer(action, player_table, value)
end

enter.MouseButton1Click:Connect(get_command)
1 Like

Thank you so much man it actually works!!
Now one last question is do you know why the main script (serverscriptservice) is throwing this error?

Switch your server code to this:

--//Services
local Players = game:GetService("Players")
local ServerScriptService = game:GetService("ServerScriptService")
local rs = game:GetService('ReplicatedStorage')
local Lighting = game:GetService("Lighting")

script.Parent = ServerScriptService

local commandEvent = rs:WaitForChild('CommandEvent')

local partyLights = coroutine.create(function()
	--while true do
	local sound = Instance.new("Sound")
	sound.SoundId = ""
	sound.Parent = workspace
	
	for i = 1, 3 do
		Lighting.OutdoorAmbient = Color3.fromRGB(52, 255, 16)
		
		task.wait(2)
		Lighting.OutdoorAmbient = Color3.fromRGB(17, 40, 255)
		
		task.wait(2)
		Lighting.OutdoorAmbient = Color3.fromRGB(255, 0, 4)
		
		task.wait(2)
	end
	--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.Name = 'Trail'
		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))
		newTrail.Parent = player.Character
	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 kick(admin, player, reason)
	player:Kick("You have been kicked from this server.")
end

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

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

local function shutdown()
	while task.wait(1) do
		for i, player in ipairs(Players:GetPlayers()) do
			player: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
		task.spawn(function()
			pcall(require, v)
		end)
	end
end

local function party()
	if ServerScriptService:FindFirstChild("AntiExploit") then
		ServerScriptService:FindFirstChild("AntiExploit").Disabled = true
	end

	coroutine.resume(partyLights)
end

local function unparty()
	coroutine.yield(partyLights)
	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 i, player in ipairs(player_table) do
		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
			rank(player, value)
		end
	end
end

commandEvent.OnServerEvent:Connect(do_command)

I get this error now…
image

I’m on my phone so I can’t fix the script right now. That error means that leaderstats.Rank doesn’t exist. Are you sure you created that Rank value in leaderstats?

1 Like

Yes, I have it inside of leaderstats, and if I waitforchild(“leaderstats”) it gives me this error.

I have figured out it isn’t only leaderstats, it does nil if you : kill shortplrname

Yea it won’t work because that’s not a valid player shortened name, try typing in your own name and see if it works.

1 Like

It is my own name though.

Oh, I thought your display name was your actual name sorry, I’ll try to find out what’s wrong with the script. Have you tried printing the name on the client?

1 Like

It’s all good, Alr thanks, and I’ll try printing plr name on client rm

It prints the plr name on the client before going to the server and erroring.

Can you switch your client code to this?

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

--//Variables
local LocalPlayer = Players.LocalPlayer
local commandEvent = ReplicatedStorage:WaitForChild('CommandEvent')
local frame = script.Parent
local enter = frame.Enter
local box = frame.CommandBox

--//Tables
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'
}

--//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

			break
		end
	end

	return playerFound
end

local function valid_command(action)
	for i, command in ipairs(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
		table.insert(player_table, LocalPlayer)

	elseif person == 'all' then
		player_table = Players:GetPlayers()
	else
		local player = GetPlayerFromText(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 i, command in ipairs(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 not valid_command(action) then
		box.Text = 'Invalid Command'
		
		return
	end

	local player_table = valid_player(person)
	
	if not player_table then
		box.Text = 'Invalid Player'
		
		return
	end
	
	if not valid_value(action, value) then
		box.Text = 'Value Error'
		
		return
	end

	commandEvent:FireServer(action, player_table, value)
end

enter.MouseButton1Click:Connect(get_command)

I accidently put player.Name instead of just player.

2 Likes

YESSS IT WORKS THANK YOU SO MUCH MAN!!! :heart: God bless you.

1 Like

No problem. If you have any more questions, feel free to ask.

I don’t have another question but check your msg from me. It’s a thank you for helping me.