Trying to FindFirstChild a string

What I am trying to create is a simple admin system using player.Chatted
However, I am facing a problem where I cannot find the player through a string
My current script is

local players = game:GetService("Players")

local KillCommand = "/kill" --Kill Command

local function FindPlayerByName(NAme)
	print(NAme.Name)
	print(NAme)
	print(NAme.Parent)
	print(type(NAme))
	if game.Workspace.WorkspacePlayers:FindFirstChild(NAme) then 
		print("a")
		return players[NAme]
	else
		warn(NAme.." not found in Folder")
	end
	for _, PlayerToBeKilled in pairs(players:GetChildren()) do
		if PlayerToBeKilled.Name:sub(1, NAme:len()):lower() == NAme:lower() then
			return PlayerToBeKilled
		end
	end
end

local function onPlayerChatted(player, message, recieptiant)
	if message:sub(1, KillCommand:len()):lower() == KillCommand:lower() then
		local PlayerKilledName = message:sub(KillCommand:len() + 1)
		local PlayerToBeKilled = FindPlayerByName(PlayerKilledName) 
		if PlayerToBeKilled then
			local WorkP = game.Workspace.WorkspacePlayers:FindFirstChild(PlayerToBeKilled)
			print(WorkP)
			local Humanoi = WorkP:FindFirstChild("Humanoid")
			Humanoi.Health = 0
		end		
	end
end

local function AddedPlayer(plr)
	plr.Chatted:Connect(function(...)
		onPlayerChatted(plr, ...)
	end)
	plr.CharacterAdded:Connect(function()
		wait(1)
		local WorkP = game.Workspace:FindFirstChild(plr.Name)
		WorkP.Parent = game.Workspace.WorkspacePlayers
	end)
end

players.PlayerAdded:Connect(AddedPlayer)

So basically when I do /kill TwyPlasma the following output from the prints are (put in comments)

print(NAme.Name) --nil
print(NAme) --TwyPlasma
print(NAme.Parent) --nil
print(type(NAme)) --string
if game.Workspace.WorkspacePlayers:FindFirstChild(NAme) then  --TwyPlasma not found in folder
	print("a")
	return players[NAme]
else
	warn(NAme.." not found in Folder")
end

I have searched through the string documentary in Roblox Developer Hub and searched through the devforum but I do not understand or the questions and solutions are not those that I am looking for.

So how do I solve this?

1 Like

Does game.Workspace.WorkspacePlayers have a child named that?

Also use players:GetPlayers() instead of players:GetChildren() instead.

1 Like

Yes, it does, the player’s character is added to the folder every time after the character is added to workspace

Edit: Also I tried players:GetPlayers() but it did not work

1 Like

As the variable KillCommand has the length of 5, this slices the string starting at the 6th character of the string to the end so /kill example will be " example" with a leading whitespace and I assume there isn’t any children named " TwyPlasma" with a leading whitespace. Start it by the 7th character (by adding it by 2 instead of 1) instead: local PlayerKilledName = message:sub(KillCommand:len() + 2)
Or use string patterns to get the username, this checks if the string starts with /kill then with at least 1 spaces then captures the rest of the pattern which I presume is the username.

local PlayerKilledName = message:match("^/kill%s+(.*)")
if PlayerKilledName then
		local PlayerToBeKilled = FindPlayerByName(PlayerKilledName) 
		if PlayerToBeKilled then
			local WorkP = game.Workspace.WorkspacePlayers:FindFirstChild(PlayerToBeKilled)
			print(WorkP)
			local Humanoi = WorkP:FindFirstChild("Humanoid")
			Humanoi.Health = 0
		end
end
1 Like