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

How to shorten a player's name in an admin commands system?

Hello.

I've tried looking on Youtube and Scripting Helpers, but I can't seem to find a way that works with my script.

Here is my script:

admins = {"bluestreakejjp","InfamousTurtle","Admin2","Admin3","etc."}
game.Players.PlayerAdded:Connect(function(plr)
	for i,v in pairs(admins) do
		if plr.Name == v then
			plr.Chatted:Connect(function(msg)
				if msg:lower():sub(1,6) == ";stats" then
					local player = msg:sub(8)
					 local found = game.Players:FindFirstChild(player)
					if found then
						local guiClone = game.ServerStorage.StatEditor:Clone()
						guiClone.Parent = plr.PlayerGui
						local StatEditor = plr.PlayerGui.StatEditor
						StatEditor.Frame.TextLabel.Text = ("Setting "..player.."'s leaderstat")
						StatEditor.String.Value = player
					end
				end
			end)
		end	
	end
end)

Help would be appreciated.

Thanks

4 comments

0

I'm not sure I understand what you mean. You want a command that will set a player's name to a nickname you define? The code you posted seems to be a command that sets a player's leaderstats.

0

It is, but instead of having to type out a player's whole name, I'd only have to type two or three characters of it. I know this involves string manipulation, but I don't know which method that I should use, or how to use that method.

0

string.sub and string.len

0

I figured I'd have to use string.len. How would I use it though?

1 answer

✓ Accepted answer

My idea for your situation is this:

You want to search through every player in the game and see if anyone's name starts with the input that the user provides.

I'm assuming here that you are only accepting inputs that are the beginnings of a player name, for example ;stats blu is accepted but ;stats streake is not. If you'd like to accept the latter, you will have to check if the input string matches ANY part of a player name. This isn't much different but it's up to you which you prefer.

To do this, an important tool you will need is string.sub which has the following parameters:

string s - the string you wish to manipulate

int i - the position you want your substring to begin at

int j [optional] - the position you want your substring to end at (inclusive)

Now, you need a way to search the input through every player. This can be done using Players:GetPlayers(). Personally, I would have a function that would return a table of all users that match the input. My function would look something like this:

local function getRequestedPlayers(requestedName)
	local returnPlayers = {}
	for _, player in pairs(Players:GetPlayers()) do
		if string.sub(player.Name, 1, #requestedName) == requestedName then
			-- I need to string.sub the name because I want to check if they both start the same
			-- You have options here, you can either return all the possible players or
			table.insert(returnPlayers, player)

			-- return the first player that matches the input name
			return player

			-- !CHOOSE ONE OF THE METHODS, NOT BOTH!
		end
	end
	return returnPlayers
end

As shown above, it's your decision if you want all the players that match the input or just the first one.

Now, you can replace line 8 in your code with getRequestedPlayers(player) and work with that.

Hope this helps.

EDIT:

If you choose to go with the method that accepts every player that matches the input (for example, ;stats ad would request admin1, admin2, admin3, then you can simply use a for loop immediately after requesting players like so:

if #foundPlayers > 0 then
	for _,foundPlayer in pairs(foundPlayers) do
		-- apply stat changes here
	end
end

1 comment

0

Thanks!

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