Math.Random in Module Script

Hello I ran into this problem, so I whenever you touch the certain part you get a point. I have it in a module script the amount that you get per touch so I can change it anytime i want without going into every script. I want the amount of points to be random from 3-5 but it only choose one number. I want it to randomly choose but not the same number, any help is appreciated.

Module Script:

local GameStats = {
	["Winter"] = math.random(2,5); 	
}
return GameStats

Script

local V_ = game:GetService("ServerScriptService")
local PopIt = script.Parent
local db = false
local B_ = game.ServerScriptService.Statistics
local M_ = require(B_.GameStats)
local PointsForPop = M_.Winter
local Cooldown = M_.CooldownForSmall

--//Services
local Players = game:GetService("Players")

--//Variables
local Part = script.Parent

--//Controls
local debounce = false
local cooldownTime = 1

--//Functions
Part.Touched:Connect(function(hit)
	local Player = Players:GetPlayerFromCharacter(hit.Parent)

	if Player and not debounce then
		debounce = true

		Part.Transparency = 1
		Part.CanCollide = false

		Player.leaderstats.Pops.Value += PointsForPop

		task.wait(cooldownTime)
		debounce = false
		Part.Transparency = 0
		Part.CanCollide = true
	end
end)

This only evaluates the math.random function call once - especially if the function is not injective.

You could call math.random inside the anonymous closure part.touched:Connect(function(hit) block rather than only evaluate PointsForPop.

1 Like

i figured it would just call once, anyways, how should i do it?

make the module into a function that u call so that every time u call it to add points, it’s a different value than before

1 Like

how would i do that, im not that good with functions in module scripts

Something like this

--[module script]
local module = {}

function module:GetPointsForWinter()
    return math.random(2,5);
end

return module


--[script]
local M_ = require(B_.GameStats)

Part.Touched:Connect(function(hit)
    Player.leaderstats.Pops.Value += M_:GetPointsForWinter()
end)

So, everytime u call module:GetPointsForWinter(), the math.random(2,5) part will be different than the one before. Hopefully this does the trick

1 Like

Thank you so muchh now I know sort of how functions work in module scripts