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

Value of StringValue won't change?

Asked by
Spoookd 32
5 years ago
Edited 5 years ago

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

Log in to vote
0
Answered by 5 years ago

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

0
After I changed it to that, it says: Workspace.CurrentMap.Script:23: bad argument #2 to 'random' (interval is empty) Spoookd 32 — 5y
0
Try printing #choices after for loop and tell me what it prints. KingLoneCat 2642 — 5y
0
it printed 0 Spoookd 32 — 5y
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. KingLoneCat 2642 — 5y
View all comments (4 more)
0
Okay thats fixed but now it says this: Workspace.CurrentMap.Script:25: bad argument #3 to 'Value' (string expected, got nil) Spoookd 32 — 5y
0
Try printing choices[picked]. Is #choices still 0? KingLoneCat 2642 — 5y
0
It for some reason prints table: 2FF9A76C Spoookd 32 — 5y
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 Spoookd 32 — 5y
Ad

Answer this question