How can I stop a module script?

I have two rain module scripts that are running. When I set it to day I want those module scripts to then stop running how can I do this?

1 Like

Rain Module:

local Raining = false

RainModule:SetRaining(bool)
    Raining = bool
end

RunService.Heartbeat:Connect(function()
    if Raining then
        -- rain
    end
end)

return RainModule

Script that sets day:

local RainModule = require(RainModule)

Lighting.TimeOfDay = 12 -- set day
RainModule:SetRaining(false) -- turn off rain
2 Likes

So how are you setting this bool? your using a local variable but then how can I set it false in other scripts?

1 Like

If your module is controlling the rain, then you can just require the module in any script and use the functions in the module table to toggle rain

No like I am now using and if statement on the module and how could I set the variable in the module to false or true?

They probably use a while loop or RunService connection, inside you can check if a bool is set to true, if it is then let the module add the rain.

Example:

local RunService = game:GetService('RunService')

local module = {}
module.Enabled = true

function module:Start()
    return RunService.Heartbeat:Connect(function()
        if not self.Enabled then return end
        -- add rain
    end)
end

return module
local module = require(path.to.module)
module:Start()
wait(10)
module.Enabled = false
2 Likes

Calling RainModule:SetRaining(false) would set the “Raining” variable inside the module to false

1 Like

In the example @.hell_ish provided you can call the SetEnabled function to set the variable inside the module.

Wouldn’t it make more sense to just use a while loop for something like this…?

Also everytime you call module.Start you’re initializing a new event connection and you’re not destroying the old one.

1 Like

Well does return work I’m not really good at scripting so I’m not good at modules

1 Like

Wait one thing is you can get the bool in side of the module by doing a table

Yeah pretty much, there is multiple options like wrapping a while true loop inside a coroutine instead of just using a connection.

1 Like