The easiest way to go about what you're trying to do is to use string.find. What you want do do is to loop through the list of Players in the game, and pass the string provided and the players name
function findPlayer(str)
for _, player in pairs(game:GetService("Players"):GetPlayers()) do
if string.find(player.Name, str) == 1 then
return player
end
end
return "Could not find player!"
end
What this does is for each player in the game it compares their name to the string you pass. string.find returns 0, 0 when it doesn't find a match, but it will return startPosition, endPosition when it does find it. Since you will always be starting with the beginning of the player's name, the startPosition will always be 1. This means we only have to check the first value even though two are returned
Implementing this into what you currently have would look like so:
local Players = game:GetService("Players")
function findPlayer(str)
for _, player in pairs(Players:GetPlayers()) do
if string.find(player.Name, str) == 1 then
return player
end
end
return "Could not find player!"
end
Players.PlayerAdded:connect(function(player)
if player.Name == "KDarren12" then
player.Chatted:Connect(function(msg)
if string.sub(msg, 1, 5) == ";kill" then
local playerToKill = findPlayer(string.sub(msg, 6))
if type(playerToKill) ~= "string" then
playerToKill.Character.Humanoid.Health = 0
end
end
end)
end
end)
7 comments
isent it msg.sub
u h m, i dunno i will try it
nope, doesn't work
the 6 on string.sub is space, you need to detect kill command with space ' string.sub(msg, 1, 6) == "; kill " ' and on get player use ' string.sub(msg, 7) ' i think this works.
Could you answer it and edit the script? I tried what I thought you meant, and it didn't work.
So how do I make it work without findfirstchild?
First, isolate the name you gave into its own string. Then, loop through all player usernames in the game. For each name, cut it to the same length as the one you gave, make them both lowercase using :lower() and compare them.