NPC Naming Help

I’m adding a new thing to my game where NPC’s just walk around and they will have a name, but I’m struggling on how I would actually script the naming part.

Something I’m looking for is the script will pick a number, one or two, and if it’s one then it picks a name out of the GirlNames, and if it’s two then it would pick out of the BoyNames. But then it would pick a name and would not reuse that name until all the Names have been chosen then it resets.

Hopefully that’s not too confusing, I can explain more but tysm!

Module:

return {
	GirlNames = {{
		"Girl1",
		"Girl2",
		"Girl3"
	}},
	BoyNames = {{
		"Boy1",
		"Boy2",
		"Boy3"
	}}
}

You would have to use math.random(1, 2) to pick a gender then math.Random(1,3) to pick a name out of the table! Then you would change the NPCs character name to the result

1 Like

You could probably implement it like this,

local dogs = {"ugh, not another pay to win game", "BRO WHY DO I HAVE TO PAY FOR EVERYTHING", "such money grabbers"}
local poggers = dogs[math.random(1, #dogs)]
--then do something with the  poggers and make it a name of an NPC for example npc.name = poggers

I don’t know how to deal with a module for this, but try using something like this. The “dogs” could be the GirlName or BoysName by using what @ViridianDevil said.

1 Like

read the comments below in my code

local list = {
	GirlNames = {{
		"Girl1",
		"Girl2",
		"Girl3"
	}},
	BoyNames = {{
		"Boy1",
		"Boy2",
		"Boy3"
	}}
}
local taken = {}
function getData()
	local gender = if (math.random() > 0.5) then "Boy" else "Girl"
	local _name = list[gender .. "Names"][1]
	local name = _name[math.random(1, #_name)]
	local key = gender .. name
	if (taken[key]) then return getData() end
	taken[key] = true
	return {gender = gender, name = name}
end
function removePerson(dataObject)
	local key = dataObject.gender .. dataObject.name
	taken[key] = false
end

-- example
local someone = getData() -- this data is taken -- returns {gender: string, name: string}
removePerson(someone) -- the data is removed, but the data can be generated again later -- returns nil
1 Like

Yes this is much better than what I said! @ThinkingAIINight I recommend you go with his solution rather than mine since mine does a similar thing but it’s more time consuming to make.

1 Like

Thank you so much for the help!

One thing that I noticed is that the remove NPC name doesn’yt really work as it repeats names sometimes, when really how it’s supposed to work is that once a name is used, it can’t be used again until every single name is used up inside of GirlNames/BoyNames.

I’ve changed your script up a bit but here it is. (ServerScript)

local NPCNamesModule = require(game.ReplicatedStorage.Modules.NPCNamesModule)
local CanCreateNPCs = true
local Taken = {}

function GetNPC()
	local NPCGender = if (math.random(1, 2) == 1) then "Girl" else "Boy"
	local Name = NPCNamesModule[NPCGender.."Names"][1]
	local NPCName = Name[math.random(1, #Name)]
	--local Key = NPCGender..NPCName
	if (Taken[NPCName--[[Key]]]) then return GetNPC() end
	Taken[NPCName--[[Key]]] = true
	return {NPCGender = NPCGender, NPCName = NPCName}
end

function RemoveNPC(DataObject)
	--local Key = --[[DataObject.NPCGender..]]DataObject.NPCName
	Taken[DataObject.NPCName--[[Key]]] = false
end

while CanCreateNPCs == true do
	local ChosenNPC = GetNPC()
	print("Gender: "..ChosenNPC.NPCGender.." | Name: "..ChosenNPC.NPCName)
	print(Taken)
	RemoveNPC(ChosenNPC)
	task.wait(2)
end

not working pic:
image

sir, RemoveNPC is used to make the given data obtainable again from GetNPC
if you dont want repeating data dont use RemoveNPC

Oh alright, thanks! How would I reset the Taken table when all the names have been used?

Taken={}. this will make all data obtainable again

I know but how would I detect if everything is true in the Taken table

local NPCNamesModule = require(game.ReplicatedStorage.Modules.NPCNamesModule)
local CanCreateNPCs = true
local Taken = {}

function GetNPC()
	local NPCGender = if (math.random(1, 2) == 1) then "Girl" else "Boy"
	local Name = NPCNamesModule[NPCGender.."Names"][1]
	local NPCName = Name[math.random(1, #Name)]
	--local Key = NPCGender..NPCName
	if (Taken[NPCName--[[Key]]]) then return GetNPC() end
	Taken[NPCName--[[Key]]] = true
	return {NPCGender = NPCGender, NPCName = NPCName}
end

function RemoveNPC(DataObject)
	--local Key = --[[DataObject.NPCGender..]]DataObject.NPCName
	Taken[DataObject.NPCName--[[Key]]] = false
end

function reset()
	Taken = {}
end

function check()
	local expected = 0
	for i, v in next, NPCNamesModule do
		local t = v[1]
		expected += #t
	end
	return #taken >= expected
end

while CanCreateNPCs == true do
	local ChosenNPC = GetNPC()
	print("Gender: "..ChosenNPC.NPCGender.." | Name: "..ChosenNPC.NPCName)
	print(Taken)
	RemoveNPC(ChosenNPC)
	task.wait(2)
end

use check() to detect if all names are used

Hm, I can’t get this to work, even with tweaking some stuff around.

which part didnt work


Well there can only be 6 NPCs before it resets right, so as u can see it repeats names still

Here’s my script

local NPCNamesModule = require(game.ReplicatedStorage.Modules.NPCModules.NPCNamesModule)
local NPCBodyColorsModule = require(game.ReplicatedStorage.Modules.NPCModules.NPCBodyColorsModule)
local NPCInformation = game.ReplicatedStorage.GameContent.NPCInformation
local NPC = game.ReplicatedStorage.GameContent.DefaultNPC
local CanCreateNPCs = true
local Taken = {}

function GetNPC()
	local NPCGender = if (math.random(1, 2) == 1) then "Girl" else "Boy"
	local Name = NPCNamesModule[NPCGender.."Names"][1]
	local NPCName = Name[math.random(1, #Name)]
	if (Taken[NPCName]) then return GetNPC() end
	Taken[NPCName] = true
	return {NPCGender = NPCGender, NPCName = NPCName}
end

function RemoveNPC(DataObject)
	Taken[DataObject.NPCName] = false
end

function reset()
	Taken = {}
end

function check()
	local expected = 0
	for i, v in next, NPCNamesModule do
		local t = v[1]
		expected += #t
	end
	return #Taken >= expected
end

while CanCreateNPCs == true do
	local ChosenNPC = GetNPC()
	local NewNPC = NPC:Clone()
	local NewNPCInformation = NPCInformation:Clone()
	local BodyColor = NewNPC["Body Colors"]
	local ChosenBodyColor = NPCBodyColorsModule[math.random(1, #NPCBodyColorsModule)]
	NewNPC.Parent = game.Workspace.Workspace.NPCs
	NewNPC.Name = ChosenNPC.NPCName
	NewNPCInformation.Parent = NewNPC.Head
	NewNPCInformation.Main.Username.NPCName.Text = ChosenNPC.NPCName
	BodyColor.HeadColor3 = ChosenBodyColor
	BodyColor.TorsoColor3 = ChosenBodyColor
	BodyColor.LeftArmColor3 = ChosenBodyColor
	BodyColor.RightArmColor3 = ChosenBodyColor
	BodyColor.LeftLegColor3 = ChosenBodyColor
	BodyColor.RightLegColor3 = ChosenBodyColor
	print("Gender: "..ChosenNPC.NPCGender.." | Name: "..ChosenNPC.NPCName)
	RemoveNPC(ChosenNPC)
	wait(2)
end

remove the RemoveNPC(ChosenNPC) in your while loop man

Oh well u had it here, do i also add check() aswell, if so it’s not working

check() returns a bool, if it returns “true” then all the names including boys and girls are all used. if it returns “false” then there’s unused names left

how would i use it in the script?

while CanCreateNPCs == true do
	if check() then reset() end
	local ChosenNPC = GetNPC()
	local NewNPC = NPC:Clone()
	local NewNPCInformation = NPCInformation:Clone()
	local BodyColor = NewNPC["Body Colors"]
	local ChosenBodyColor = NPCBodyColorsModule[math.random(1, #NPCBodyColorsModule)]
	NewNPC.Parent = game.Workspace.Workspace.NPCs
	NewNPC.Name = ChosenNPC.NPCName
	NewNPCInformation.Parent = NewNPC.Head
	NewNPCInformation.Main.Username.NPCName.Text = ChosenNPC.NPCName
	BodyColor.HeadColor3 = ChosenBodyColor
	BodyColor.TorsoColor3 = ChosenBodyColor
	BodyColor.LeftArmColor3 = ChosenBodyColor
	BodyColor.RightArmColor3 = ChosenBodyColor
	BodyColor.LeftLegColor3 = ChosenBodyColor
	BodyColor.RightLegColor3 = ChosenBodyColor
	print("Gender: "..ChosenNPC.NPCGender.." | Name: "..ChosenNPC.NPCName)
	wait(2)
end
1 Like

this didnt work, with no errros :frowning:
edit: i printed #taken >= expected every time it runs and it prints false 6 times and then it breaks since all the names r used up and it doesnt reset