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?
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.
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
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
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()