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
1 | function findPlayer(str) |
2 | for _, player in pairs (game:GetService( "Players" ):GetPlayers()) do |
3 | if string.find(player.Name, str) = = 1 then |
7 | return "Could not find player!" |
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:
01 | local Players = game:GetService( "Players" ) |
02 | function findPlayer(str) |
03 | for _, player in pairs (Players:GetPlayers()) do |
04 | if string.find(player.Name, str) = = 1 then |
08 | return "Could not find player!" |
11 | Players.PlayerAdded:connect( function (player) |
12 | if player.Name = = "KDarren12" then |
13 | player.Chatted:Connect( function (msg) |
14 | if string.sub(msg, 1 , 5 ) = = ";kill" then |
15 | local playerToKill = findPlayer(string.sub(msg, 6 )) |
16 | if type (playerToKill) ~ = "string" then |
17 | playerToKill.Character.Humanoid.Health = 0 |