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

How do I make a random string chooser?

I'm trying to get this NPC to get a random name when a function is called, heres my script

The names that I want to choose from are in a folder called NameList

function ChooseName()
	local names = (game.ServerStorage.NameList:GetChildren().name)
	local name = names[math.random(1,#names)]
	
	print(name)
	
	script.Parent.Name = name
end

ChooseName()

This is an error I get, Workspace.NPC.Name:4: attempt to get length of a nil value

2 comments

0

I think you can just use strings instead

0

Change line 7 to script.Parent.Name = name.Name

1 answer

You got a mistake at these lines Line 3-4

local names = (game.ServerStorage.NameList:GetChildren().name) -- GetChildren returns a table of objects. Only the objects themselves there exist, not their names as strings.
    local name = names[math.random(1,#names)] -- So you have to get .Name property here.

It should be...

local names = (game.ServerStorage.NameList:GetChildren())
    local name = names[math.random(1,#names).Name]

Final:

function ChooseName()
    local names = (game.ServerStorage.NameList:GetChildren())
    local name = names[math.random(1,#names).Name]

    print(name)

    script.Parent.Name = name
end

ChooseName()

I wrote this on mobile so if there is any mistak, please tell me.

1 comment

0

Some clarification: While GetChildren doesn't return string names by adding .Name, you call loop through the objects it returned and get it there, because the objects themselves hold that property.

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