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

Getting a players name from string.sub?

Asked by 4 years ago

So what I want to get is the players name from a string such as string.sub I've tried 3 different ways to get the player but seemed to fail. Here is what I tried:

 game.Players.PlayerAdded:Connect(function(plr)

plr.Chatted:Connect(function(msg)

local sub = string.sub(msg, 1,5)

if sub == ":kill" then

local player = string.sub(msg, 6)

local findplayer = game.Players:FindFirstChild(player)

if findplayer then

findplayer.Character:BreakJoints()

end

end

end)

end)

There is no error in output, and I've looked at the script many times but couldn't seem to find the problem.

0
I've tried WaitForChild() as well User#27158 -5 — 4y
0
what about whitespaces? User#23252 26 — 4y

1 answer

Log in to vote
0
Answered by
pidgey 548 Moderation Voter
4 years ago
Edited 4 years ago

Your script works, however, I don't think you intended it to work the way it does. Immediately followed after the player types ':kill' sub(1, 5), line 9 looks for any string after it sub(6), followed by looking for the player with that matching name, so on and so forth. I believe your issue is that you're expecting there to be whitespace inbetween your command and argument when there can't be, according to the instructions of your script. Whitespace included :kill Player Whitespace excluded :killPlayer

There are many solutions, the simplest of which can be just adding a space in your matching command

game.Players.PlayerAdded:Connect(function(plr)    
    plr.Chatted:Connect(function(msg)    
        local sub = string.sub(msg, 1,6) -- 1,6 because whitespace is also a character.   
        if sub == ":kill " then --> looks for :kill immediately followed with a space. this is 6 characters long now 
            local player = string.sub(msg, 7)  -- after the space, looks for character names  
            local findplayer = game.Players:FindFirstChild(player)    
            if findplayer then    
                findplayer.Character:BreakJoints()    
            end    
        end    
    end)    
end)

This edited script will make room for a whitespace between your command and argument on lines 3-5

Ad

Answer this question