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

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

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:

game.Players.PlayerAdded:connect(function(player)
	if player.Name == "KDarren12" then

	                player.Chatted:connect(function(msg)
	                    if string.sub(msg, 1, 5) == ";kill" and game.Players:FindFirstChild(string.sub(msg, 6)) then
	                        game.Players:FindFirstChild(string.sub(msg, 6)).Character.Humanoid.Health = 0
	                    end
	                end)
	            end
	end)

Can I get some help please?

7 comments

0

isent it msg.sub

0

u h m, i dunno i will try it

0

nope, doesn't work

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.

0

Could you answer it and edit the script? I tried what I thought you meant, and it didn't work.

0

So how do I make it work without findfirstchild?

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.

1 answer

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 -- This is to make sure it returns a player and not an error
					playerToKill.Character.Humanoid.Health = 0
				end
			end
		end)
	end
end)

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