Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a random string chooser?

Asked by 3 years ago

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

0
I think you can just use strings instead bruce_matthew 4 — 3y
0
Change line 7 to script.Parent.Name = name.Name DinozCreates 1070 — 3y

1 answer

Log in to vote
0
Answered by
Y_VRN 246 Moderation Voter
3 years ago

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.

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. Y_VRN 246 — 3y
Ad

Answer this question