How to stop a function inside a module?

Hello. I wrote a module for player diseases. For example, a player becomes infected with a disease and a function is activated. The problem is I don’t know how to stop this function.

function module.Distrese(name)
if module[name] then
clone.Name = module[name].Name
clone.Image = module[name].Image
clone.Parent = Player.PlayerGui.Diseases.DiseasesFrame
Description.Text = module[name].Description
if module[name].Vibration == true then
if vibrating == nil then
vibrating = RunService.RenderStepped:Connect(function()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(.075math.sin(45tick()), 0,0) --anglemath.sin(velocitytick())
end)
else
vibrating:Disconnect()
vibrating = nil
end
end

	if module[name].RemoveHP == true then
		while true do
			character.Humanoid.Health -= 2
			wait(1)
		end
	end
end

end

4 Likes

you should have variable inside module, and functions to change it, then while’s loop Running statement will change to false and loop will stop

Also use task.wait(), it’s higher frequence and don’t buggy like old wait()

there are a couple of ways…
using a while loop condition:

local condition = true

while condition do
--code
end

to stop the loop, you can then set condition to false:

condition = false

or you can try using coroutines:

local thread = coroutine.create(function()
 while task.wait(1) do
  --logic
 end
end)

coroutine.resume(thread)

and then you can close it later:

coroutine.close(thread) --closes the thread and puts it in a dead state.

Hope this helps!

3 Likes

corountines are pretty much unnecesary, condition will do on it’s own

yeah but might aswell tell him coroutines exist.
But yeah condition fits perfectly here.

1 Like

I don’t really understand what coroutines are. Why shouldn’t they be used here?

1 Like

pretty much, they shouldn’t, corountines are multi-thread, dev king made tutorial about them, stick to conditions, they are more efficient

OK, thank you. I would try to put it while I can. Just need to test it a little

1 Like

I have a problem. How can I pass this variable to another script?

1 Like

if you are using modules, you can make:

function module:StopDamage() condition = false end
1 Like

I do not quite understand. I put a variable in a function, but I want this variable to be changeable in localscript

function module.Distrese(name)
if module[name] then
local condition = true
clone.Name = module[name].Name
clone.Image = module[name].Image
clone.Parent = Player.PlayerGui.Diseases.DiseasesFrame
Description.Text = module[name].Description
while condition do
if module[name].Vibration == true then
if vibrating == nil then
vibrating = RunService.RenderStepped:Connect(function()
character.HumanoidRootPart.CFrame = character.HumanoidRootPart.CFrame * CFrame.new(.075math.sin(45tick()), 0,0) --anglemath.sin(velocitytick())
end)
else
vibrating:Disconnect()
vibrating = nil
end
end
task.wait()
end
end
end

1 Like

never put damage on the client side!!! it will not affect player at all!

you should create module on the server and listener that will have all player’s with effect of damage in list, then module would have 2 methoods:

function module:DealDamage()
-- code to deal damage if player is in player's list
end
function module:RemovePlayerEffect(plr:String)
  module.PlayerList[plr] = nil -- removes player from list, soo first function will not take him to damage list
end

2 Likes

How can I check if a player is on the list?

1 Like

I made tutorial about lua, in 8th tutorial at the very end you have example of trick to make that:

Just a question, is the “function” that you want to stop the RunService.RenderStepped?

1 Like

Excuse me, but I honestly didn’t understand very much. If it’s not difficult for you, explain what I did wrong. I didn’t damage the player

1 Like

Uhhh, simply, you have function that do something in while loop, and you want to stop this loop in specific case, you should create second function in your module, and this function will change condition to false, soo loop will automatically stop

No

1 Like

Thanks I got it. I’ll try to do something

1 Like

Hi again. I tried to write something, but again I didn’t understand anything.

1 Like