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?