Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

cutting off the end of something (like a player's name) with stringsub?

Asked by
KDarren12 705 Donator Moderation Voter
6 years ago

I need some help with this script I have. I wanted to say for example if the player’s name is ExampleHere i would like to say “;kill Exa” and have it kill the player, but, it doesn’t work. Code:

01game.Players.PlayerAdded:connect(function(player)
02    if player.Name == "KDarren12" then
03 
04                    player.Chatted:connect(function(msg)
05                        if string.sub(msg, 1, 5) == ";kill" and game.Players:FindFirstChild(string.sub(msg, 6)) then
06                            game.Players:FindFirstChild(string.sub(msg, 6)).Character.Humanoid.Health = 0
07                        end
08                    end)
09                end
10    end)

Can I get some help please?

0
isent it msg.sub danglt 185 — 6y
0
u h m, i dunno i will try it KDarren12 705 — 6y
0
nope, doesn't work KDarren12 705 — 6y
0
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. yHasteeD 1819 — 6y
View all comments (3 more)
0
Could you answer it and edit the script? I tried what I thought you meant, and it didn't work. KDarren12 705 — 6y
0
So how do I make it work without findfirstchild? KDarren12 705 — 6y
0
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. whenallthepigsfly 541 — 6y

1 answer

Log in to vote
1
Answered by 6 years ago
Edited 6 years ago

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

1function findPlayer(str)
2    for _, player in pairs(game:GetService("Players"):GetPlayers()) do
3        if string.find(player.Name, str) == 1 then
4            return player
5        end
6    end
7    return "Could not find player!"
8end

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:

01local Players = game:GetService("Players")
02function findPlayer(str)
03    for _, player in pairs(Players:GetPlayers()) do
04        if string.find(player.Name, str) == 1 then
05            return player
06        end
07    end
08    return "Could not find player!"
09end
10 
11Players.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 -- This is to make sure it returns a player and not an error
17                    playerToKill.Character.Humanoid.Health = 0
18                end
19            end
20        end)
21    end
22end)
Ad

Answer this question