Cant find player?

so i want to make commands but this says

			local player = string.sub(CommandBox.Text, v:len() +2 , CommandBox.Text:len())
			print(player)
			if CommandBox.Text == v .. " " .. game:GetService("Players"):FindFirstChild(player) then

17:13:06.912 Players.coolman21627.PlayerGui.Console.Console.Frame.Core:9: attempt to concatenate string with Instance - Client - Core:9

any ideas why it wont work

The error says everything you should know: it looks like you’re trying to add a string with an Instance, in this case a Player. You should get the player’s name and then add it to the string.

This should work:

local player = string.sub(CommandBox.Text, v:len() +2 , CommandBox.Text:len())
print(player)
if CommandBox.Text == v .. " " .. game:GetService("Players"):FindFirstChild(player).Name then

However, it’s still bad logic. What if the script finds no player? It’ll throw an error because you’re trying to get the name of a player that doesn’t exist. This is the better solution:

local playerNameFromUI = string.sub(CommandBox.Text, v:len() +2 , CommandBox.Text:len())
local player = game:GetService("Players"):FindFirstChild(playerNameFromUI)
if(player) {
   if CommandBox.Text == (v .. " " .. player.Name) then
      --Code
   end
} else {
   print("Player wasn't found!")
}

says attempt to index nil with name, should i use a server script?

No, that means your script isn’t finding the player. Maybe you’re not getting the correct name of the player from the UI. This error shouldn’t occur with my second version of the script, try using that one.

no it said expected “then” i can show the full script

Ah yes, apologies. I mixed up my syntax.

local playerNameFromUI = string.sub(CommandBox.Text, v:len() +2 , CommandBox.Text:len())
local player = game:GetService("Players"):FindFirstChild(playerNameFromUI)
if(player) then 
   if CommandBox.Text == (v .. " " .. player.Name) then
      --Code
   end
else 
   print("Player wasn't found!")
end

no error just didnt do anything

Yeah, you have to put whatever you wanted to do in the second if block. The script does nothing on its own because you never told us what you want to do after checking that the command box is equal to the string with the player’s name. Just replace the --Code with whatever you were trying to do.

in already tried that but still didnt work

this is the original script

local CommandBox = script.Parent.CommandBox
local commands = require(script:WaitForChild("Commands"))

CommandBox:GetPropertyChangedSignal("Text"):Connect(function()
	for _, v in pairs(commands.Commands) do
		if CommandBox.Text ~= v then
				if CommandBox.Text == v .. " --detect name here" then
					CommandBox.TextColor3 = Color3.fromRGB(255,255,255)
				else
					CommandBox.TextColor3 = Color3.fromRGB(255,0,0)
				end
			else
				CommandBox.TextColor3 = Color3.fromRGB(255,255,255)
		end
	end
end)

Try putting a print (commands.Commands) before the for loop see what it prints.

@coolman21627
Are you using a Local Script or a Server Script? Is it a textbox?