How to get random number in Module Scripts

Hello devs!

I want the weapon to deal random damage, but when it draws a number it is the same.

Module Script:

local Global = {}

Global.Melee = {
	Spear = {
		Damage = math.random(2, 3),
		Cooldown = 5
	},
}

return Global

When your code compiles, the damage is given a random value from 2 to 3, then the code will not pick a random number again. In other words, the damage will be given a value of 2 or 3 and keep that value after running the code.
You need to generate a random number every time the tool is used. Instead of having a damage key in your dictionary, you can use a range element in the dictionary and call math.random on that range every time the tool is used:

Spear = {
    range = {2, 3},
    cooldown = 5
}
 -- This isn't an actual event; i'm just using it as an example
spear.Used:Connect(function()
    local damage = math.random(Spear[range][1], Spear[range][2])
    -- deal damage and everything else
end)
2 Likes