Math.random unreliable now?

Apparently in my script if I try to index something in a dictionary using math.random, the script breaks, however, if I put math.random in a variable and do the same thing, all of a sudden it works. Is this some sort of roblox bug?

The dictionary:

[1] = {
	Action = "Decoration",
	[1] = {
		DecorationStorage.Grass,
		DecorationStorage.SmallTree,
		DecorationStorage.Tree,
	},
	[2] = {
		DecorationStorage.FunkyPineTree,
		DecorationStorage.Grass,
	},
	[3] = {

	},

This is the part that breaks:

Type[player:WaitForChild("CurrentWorld").Value][math.random(1, #Type)]:Clone()

Results to: Screenshot 2021-05-28 221648

However

local Numb = math.random(1, #Type)
Type[player:WaitForChild("CurrentWorld").Value][Numb]:Clone()

Results to: Screenshot 2021-05-28 222009

Strange?

I think it’s just a logic error. You have to remember that math.random in itself is a function. Although, at first glance it should work. But in terms of performance it would seem inefficient. Then again, it’s been a while since I’ve done much of anything on Roblox. So I could be wrong.

At least in the code provided, there are no items in table 3. This would mean that regardless of how math.random is used, if table 3 is the table being queried, :Clone() won’t have anything to pull from and will throw an error.

Given that’s your only change in your code it would seem like a race condition.

Try:
wait(1)
Type[player:WaitForChild(“CurrentWorld”).Value][math.random(1, #Type)]:Clone()

If that works then you’re indexing something that hasnt had the time to appear yet. Or perhaps CurrentWorld has not been changed to desired value yet.

Unfortunately I need speed with this kind of script, waiting would directly affect the player’s experience, unless the have the patience to wait a second lol

3 Shouldn’t be a problem considering that by then the player’s current world value would be used to index in that layer, right now it’s set to one and there is no way to increase it

Then print

  • Type[player:WaitForChild(“CurrentWorld”).Value]
  • math.random(1, #Type)

Let us know what it says. (Expand the table before u screenshot)

print(Type)
Type = Type[player:WaitForChild("CurrentWorld").Value]
print(Type)
Type = Type[math.random(1, #Type)]:Clone()
print(Type)

Screenshot 2021-05-28 224742

You say you print Type 3 times, but your output only shows 2 prints.

Also, overriding Type with a new variable everywhere is very confusing.
Also I just wanted this:

print(Type)
local index1 = player:WaitForChild("CurrentWorld").Value
local index2 = math.random(1, #Type)
print(index1, index2)

Type[index1][index2]:Clone() --This is where it should error. include that.

It actually seems like separating the searches like that fixed the problem, not sure why, but I’ll take it!
Edit: ran the test again with Clone(), it still works

print(Type)
			
local index1 = player:WaitForChild("CurrentWorld").Value
local index2 = math.random(1, #Type)
			
print(index1, index2)
print(Type[index1][index2])
							
return Type[index1][index2] <-- return there because I wrapped the test in a function

Results: Screenshot 2021-05-28 230443

Just a friendly reminder that math.random is deprecated now and Random.new() is the new preferred method of random number generation! Honestly, haven’t looked over it super carefully but in the past, issues I’ve had with math.random() were solved with Random.new()

2 Likes

Hmm, maybe that might be part of the reason it broke in the first place? I’ll just start replace the old math.randoms