Help with math.random

Am i doing this right? I assume i could just plug math.random in and it should work?

local dropInfo = {

	["MediumStone"] = {
		["Health"] = 3,
		["DropName"] = "Stone",
		["DropAmount"] = math.random(2, 5),
		["RespawnTime"] = 15,
		["HitSound"] = "rbxassetid://8493437098",
		["DestroySound"] = "rbxassetid://8493436735"

I don’t know if you can assign a key a function call (never tried it) but what you can do instead is something like this outside of the dictionary.

dropInfo["MediumStone"]["DropAmount"] = math.random(2,5)

This would do the exact same thing.

if you tested it and it works then it works

print(dropInfo.MediumStone.DropAmount)

This should be fine. As long as what you want is a random value (constant after running).

A function call is not an object – when you call a function, it returns the raw value as if you were simply assigning any value to a variable (there will be no trace that any function was called).


OP, the following code will work, but math.random is only being called once at the beginning of runtime. Assuming you want a random amount of drops every time the object is collected, I would suggest having a minimum value and a maximum value, and then calling math.random when the object is collected. For example:

local dropInfo = {

	["MediumStone"] = {
		["Health"] = 3,
		["DropName"] = "Stone",
        -- Minimum and Maximum values
		["DropMin"] = 2,
		["DropMax"] = 5
		["RespawnTime"] = 15,
		["HitSound"] = "rbxassetid://8493437098",
		["DestroySound"] = "rbxassetid://8493436735"
local function collect(obj)
    local dropAmount = math.random(obj.DropMin, obj.DropMax)
    -- Continue code
end
1 Like