I tried making a name picking script, where it randomly picks a name from the table and prints it, however it isn’t working when I call the function on the server script, and just prints “nil”. Can anyone seem to help with this? Thanks!
If you do @TheDCraft’s method then you won’t be able to print the value they store, but rather just the name. What you can do to make your current code work is this:
local randomNum = math.random(#names)
local chosenNameValue = nil
local counter = 1
for i, v in pairs(names) do
if counter == randomNum then
chosenNameValue = v
break
end
counter++
end
This is not a normal table, and more of a dictionary. You can try something like this:
local onlyGrabNames = {};
for key, value in pairs(names) do
-- Actual name would be key
table.insert(onlyGrabNames, key);
end
local chosen = onlyGrabNames[math.random(1,#onlyGrabNames)]
print(chosen)
Now say, you want 1 or 2, to change the chances, you can do:
local onlyGrabNames = {};
for key, value in pairs(names) do
-- Actual name would be key
for index = 1, names[key] do
table.insert(onlyGrabNames, key);
end
end
local chosen = onlyGrabNames[math.random(1,#onlyGrabNames)]
print(chosen)