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

How to allow shortened usernames?

I'm trying to edit the script of a model I've been using for a while. I want the script to be able to detect shortened versions of usernames (So, rather than having to type in "bs joemcburger" I could type "bs joe"

This is the code:

print("Jail") 

jail = script.Parent 

permission = { "joemcburger","burninrubber02","ipwnuFOOLZ"} 


function check(name) 
if table.maxn(permission) == 0 then return true end 
for i = 1,#permission do 
if (string.upper(name) == string.upper(permission[i])) then return true end 
end 
return false 
end 

function Jail(name) 
local player = game.Players:FindFirstChild(name) 
local torso = player.Character:FindFirstChild("Torso") 
if torso == nil then return end 
local location = {jail.Position} 
local i = 1 

local x = location[i].x 
local y = location[i].y 
local z = location[i].z 

x = x + math.random(-1, 1) 
z = z + math.random(-1, 1) 
y = y + math.random(2, 3) 

local cf = torso.CFrame 
local lx = 0 
local ly = y 
local lz = 0 

torso.CFrame = CFrame.new(Vector3.new(x,y,z), Vector3.new(lx,ly,lz)) 
end 

function onChatted(msg, recipient, speaker)

local source = string.lower(speaker.Name)
msg = string.lower(msg) 

if not check(source) then return end 

if string.match(msg, "bs") then  -- Between the brackets, this is what you will say when you want the person teleported. So its like, Person1:jail NUBLET then the nub would teleport to the brick this script is in.
local players=game.Players:GetChildren() 
for i=1, #players do 
if string.match(msg, string.lower(players[i].Name)) then 
Jail(players[i].Name) 
return 
end 
end 
end 
end 

function onPlayerEntered(newPlayer) 
newPlayer.Chatted:connect(function(msg, recipient) onChatted(msg, recipient, newPlayer) end) 
end 

game.Players.ChildAdded:connect(onPlayerEntered)

2 comments

0

Put in code block format please.

0

Edited for code block. I would recommend tabbing your code properly.

1 answer

✓ Accepted answer

The easiest way to do this is to go and do a :find() function to find a portion of the name matching what you said

function findPlayer(name)
	for _, player in pairs(game.Players:GetPlayers()) do
		if player.Name:lower():find(name:lower()) then
			return player;
		end;
	end;
end;

Adding this in and doing findPlayer("joe") would search for a player name containing joe. This is useful for the partial name you want to search for and will return the player.

3 comments

0

Where in my code would I put this? And how would I go about getting the rest other functions to use this?

0

Pretty much this is a separate function you put at the top of your code

0

And instead of doing the 'if string.match(msg, players[i].Name) then' you would do 'if findPlayer(msg:sub(4)) then' This this will find the player who's name includes whatever is on or after the 4th character of the string.

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