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

Attempt to index nil with 'random'? what

So I have this script which locally sets the name of workspace every 0.05 seconds so no exploiters can access it. Though, I do get an error.

local scriptnames = {"aHaHaHa","ahHhaHAhAh","HAhAhah","CorRuPpTeD","sdhPdh","hoIdhiu","HUDiHDos","Dh9hdfi","Dippja","CorUppTeddD"}
while true do
wait(0.1)
game.Workspace.Name = scriptnames.math.random(0,#table)
print("Successfully changed workspace name to "..game.Workspace)
end

ok goodnight

2 comments

0

Next time describe your problem explicitly. And also make sure to show the error.

3 answers

✓ Accepted answer

For some reason the past two answers are "not working" according to OP so I am posting. (Despite Feahrens answer being correct :l)

Question

Attempt to index nil with 'random'?

Solution

The error is on line 4.

game.Workspace.Name = scriptnames.math.random(0,#table)

The issue is that you're attempting to index table scriptnames with key math and then index that with random. The issue is that there is no index math inside table scriptnames.

I assume this code is meant to pick a random value from scriptnames. If you want to pick a random value from an array you will want to first generate a random number such that the lowest value is the 1st index of the array the maximum value is the amount of things in said array. This can be achieved by doing math.random(1, #array).

Now using this random number we can index our array with the number to get a random index. array[math.random(1, #array)]`

Using this knowledge you should be able to come up with the code below, which solves your problem:

game.Workspace.Name = scriptnames[math.random(1, #scriptnames)]

This will index scriptnames with a random number between 1 and the amount of indices in scriptnames, then assign the Name property of the Workspace service to that value.

there is no variable named table, it is also a reserved keyword so there can never be. You must pass the actually array reference name that is assigned to the table

math.random(1,#scriptnames) --// There is no index 0 in any table

1 comment

0

table isn't a keyword, nor is it reserved. It is a predefined table in the environment. It can be overwritten/shadowed.

This should work: EDITED

local scriptnames = {"aHaHaHa","ahHhaHAhAh","HAhAhah","CorRuPpTeD","sdhPdh","hoIdhiu","HUDiHDos","Dh9hdfi","Dippja","CorUppTeddD"}
while true do
wait(0.1)
game.Workspace.Name = scriptnames[math.random(1,#table)].Name
print("Successfully changed workspace name to "..game.Workspace.Name)
end

If this helped please up vote and accept this answer.

All the best,

PrismaticFruits

1 comment

0

bad arguement 2 interval is empty

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