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

Value of StringValue won't change?

I am trying to choose maps from the ReplicatedStorage and set the value of a StringValue to the name of the map:

local choices = {}

function choose()
	for i = 1,#maps do
		if maps[i]:IsA("Model") then
			table.insert(choices,maps[i])
		end
	end
	local picked = math.random(1,#maps)
	chosen.Value = choices[picked].Name
end

But an error pops up every time and I can't figure out why: Workspace.CurrentMap.Script:24: attempt to index field '?' (a nil value)

1 answer

Hi Spookd,

The problem with that is that when you find a number that you randomly generated, you used the wrong table to generate it from. You said:

local picked = math.random(1, #maps);

###But, it should be:

local picked = math.random(1, #choices);

###It needs to be this because you're choosing from choices, not maps. They don't have the same amount of values. The reason they don't have the same amount of values is because maps has other values, not just model instances, and choices only has the models inside of maps. Therefore, I am assuming they have different values because you had an if statement that checked if the value was a model, and then added it to choices.

So, if you make it:

local picked = math.random(1, #choices);

it should be fixed.

##Hope I helped, and have a wonderful day/night.

Thanks,

Best regards,

~~ KingLoneCat

8 comments

0

After I changed it to that, it says: Workspace.CurrentMap.Script:23: bad argument #2 to 'random' (interval is empty)

0

Try printing #choices after for loop and tell me what it prints.

0

it printed 0

0

That's the problem.You can't pick a map if there isn't one in the choices table. Make sure maps has models under it.

0

Okay thats fixed but now it says this: Workspace.CurrentMap.Script:25: bad argument #3 to 'Value' (string expected, got nil)

0

Try printing choices[picked]. Is #choices still 0?

0

It for some reason prints table: 2FF9A76C

0

Its probably because i had to switch the [#choices + 1] = maps[i] around to maps[i] = #choices + 1 because when I do that it says that it got an unexpected '[', but I dont know how to switch that around and I dont know if it is the problem either way

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