Need Help creating a name script

Hello. I am working on an SWRP server, and I need help with making a name creation system. In a nutshell, what I am aiming for, is when a player first joins, he/she will be prompted with a UI to enter a name, and then after that a string of 5 randomly generated (and preferably unique) numbers will be prefixed to the name. After doing this, they cannot change their name using /rpname or any of the other darkrp name changes. Any help is greatly appreciated, thanks!

bump

GM:PlayerInitialSpawn

vgui.Create

Player:SetPData

Player:GetPData

Add these together, then add some Lua and a bit of magic and you’re done.

I’m new to lua, some extended help would be nice, however I will do my best.

Also using SQL, how could I check if a uid exists, so instead of using initial spawn, it will check if they have data on the table. (This way, I don’t have to completely reset playerdata)

[editline]16th September 2017[/editline]

I have this, however I doubt it would work to check if the data already exists:



function checkForName(ply)
	sql.Query( "SELECT uid FROM swrp_playernames WHERE uid = " .. ply:UniqueID() .. ";")
end)


Does the gamemode even use the built-in SQL system like that? I mean you can literally count the number of rows where rows > 1 is valid data.

Maybe

sql.QueryValue? Check whether the return value is valid and boom, valid entry.

Yes, the base gamemode is DarkRP, and I want to make sure that a UID has data, not that there IS data, so if someone joins the first time but crash or leaves without doing the UI, they will be prompted with it until they have data on the table.

Is it just me or is PData in any case easier to use than SQL Queries for a beginner to coding?

i dunno, is it?

Seeing that ply:GetPData( “FirstJoin”, false ) and ply:SetPData( "FirstJoin, true ) is basically doing the same as a big SQL Query but way simpler I suggest you use that instead.

–snip–

[editline]16th September 2017[/editline]

Aha, I understand. So after they complete the form, we would run ply:SetPData( “FirstJoin, true” )

Two things, recommend you use true and false here and also choose a more original name, you don’t want to overwrite someone else’s data. uid, name, money, things like that are too common. I always like to choose names that are hardly ever chosen, such as GLXY_MONEYDATA for example. You might wanna go with like Jonnyetiz_UID

[editline]17th September 2017[/editline]

The point is also, if you’re a starter this might not be the easiest project to make and there are solutions sort of like this already out there.

I went with ply:GetPData( “HasName”, false )

Generally the data I will need to store will be the name and the 5 numbers that come before the name (I don’t think I should need the UID anymore with ply:GetPData, as the only reason for the UID was for SQLite to read who’s data is whose)

In that case store the whole thing, why would you store both Galaxy and 28393 for example if you could also just store the string “Galaxy#28393” and then use

string.Split and split it where you have put the #, plus it looks cool because B.Net and Discord uses the #.

Could I do it without the # in between, and also remember how I want the number to be randomly generated between 11111 and 99999.

In a nutshell, They enter their name in the gui(I figured that part out)
The GUI takes that name, and then generates the random number
Saves the data, then whenever they are ingame their name will display as

Name

[editline]16th September 2017[/editline]

Here’s what I have so far:

cl_namegui.lua



function DrawGUI()
	local frame = vgui.Create( "DFrame" )
	frame:Center
	frame:SetSize( 200, 400 )
	frame:MakePopup()
	frame:ShowCloseButton( false )
	
	--Description Text
	description = vgui.Create( "DLabel", frame )
	description:SetPos( 50, 50 )
	description:SetText( "Enter your desired name. Do not worry about anything other than your name, as you will be automatically assigned a number!" )
	description:SizeToContents()
	
	--Where the name will be entered
	name_entry = vgui.Create( "DTextEntry", frame )
	name_entry:SetSize( 100, 20 )
	name_entry:SetPos( 50, 250 )
	
	--Button to submit the name to the server.
	submit_button = vgui.Create( "DButton", frame )
	submit_button:SetSize( 100, 50 )
	submit_button:SetPos( 50, 350 )
	submit_button:SetText( "Submit!" )
	submit_button.DoClick = function()
		net.Start ( "send_name" )
			net.WriteString( name_entry:GetValue() )
		net.SendToServer()
	end
	
end
net.Receive( "opennamegui", DrawGUI )


and sv_namingsystem.lua



util.addNetworkString ( "opennamegui" )

plyNumber = math.random( 11111, 99999 )

-- Check if a UID has a name assigned in case the player crashes or does not complete the form on first spawn.
if ply:GetPData( "HasName", false )
then
net.Start( "opennamegui" )
net.Send( ply )
end

net.Receive( "send_name", ply:SetPData( "HasName", true ) )


You could but it would be easier to store with a seperator. For the random part I guess you should make a table in your database which contains the already taken IDs and have a math.random( 0, 99999 ) run everytime until it finds an ID that hasn’t been taken yet. It’s not efficient but it should work

Wait so if Im using PData, where is that database?

PData does SQL but more compact. It’s in the same place as your DB would normally be

What table would it be under though

Also, I don’t know how to test the script

[editline]17th September 2017[/editline]

Getting the error:



[ERROR] lua/sv_namingsystem.lua:6: attempt to index global 'ply' (a function value)
  1. unknown - lua/sv_namingsystem.lua:6


when I execute sv_namingsystem.lua (Using same exact code as seen above)

I think you need to follow a basic tutorial for GLua first seeing the kinds of issues you get. Anyways the specific issue you get has to do with ply not being predefined, you first have to define ply somewhere, for example in an InitialSpawn hook by using


hook.Add( "PlayerInitialSpawn", "J_FirstSpawn", function( ply )

This seems to have fixed it, however now I’m trying to figure out a reliable way of testing the script.