Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Getting a players name from string.sub?

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.

2 comments

0

I've tried WaitForChild() as well

0

what about whitespaces?

1 answer

✓ Accepted answer

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

This discussion is preserved as it was. New answers, comments, and votes are closed.