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!
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
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.
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
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.
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
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
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
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
this didnt work, with no errros
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