I wanted to create a system where the player could create his own name and if a name was already being used, an error message “this name is already being used” would appear, if that is possible how can I achieve this?
Is this across a single server or the entire game?
Either way, you would need to store a list of all of used names (use datastores to carry across servers).
Use table.find(nameList, name)
to determine if the name has been used.
I would suggest storing these names formatted as string.lower()
or string.upper()
so the same names with different capitalization can’t be chosen.
It is in the entire game but how would I go about using table.find globally?
Set a key in a datastore to the name. When a player tries to pick a name, use getasync() and check if it returns a true value. When a player picks a name, use setasync() and set that value to true
You would need to use datastores for this.
You can really do this multiple ways, but somehow store all of the used names, and before confirming a player’s name, reference the stored names to make sure it hasn’t been used.
I don’t personally know all of the ins and outs of datastores, but you can do what @ScriptideALT said, and save a boolean under a key, although I’m not sure if that’s a good practice or not (again, not the best with datastores). Instead of a list that you use table.find
on, you can create a dictionary and do playerNames["MayorGnarwhal"] = true
, which might be a bit more efficient.
Either way, you will be needing datastores.
The problem with your method is that in a large game, the table playernames will grow too large to run getasync() or setasync() on. It is better to store each name individually under seperate keys
local NameTable = {}
Event:Connect(function()
local IsTaken = false
local PlayerRequestName = (What they inputted)
for i,v in pairs(NameTable) do
if v == PlayerRequestName then
IsTaken = true
end
end
if IsTaken == true then
--Tell them its taken
else
table.insert(NameTable, #NameTable+1, PlayerRequestName)
--Do whats suppose to happen if its not taken (Insert the name into the table so no one can pick it.)
end
end)
---Datastore not incorporated into the code / Basically a check the server for what you inputted.
---Also, don't forget to run through the table to take non taken names out of the table.