How to destroy a function or stop the function

I am testing something that can stop a function, but it is not working .

here is a server script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local StartQuestRemote = ReplicatedStorage:WaitForChild("StartTheQuest")
local GiveMoneyRemote = ReplicatedStorage:WaitForChild("GiveMoney")
local StopQuestRemote = ReplicatedStorage:WaitForChild("StopTheQuest")
local Module = require(ServerStorage:WaitForChild("ModuleScript"))

StartQuestRemote.OnServerEvent:Connect(function(Player,StartOrStop)
	
	if StartOrStop == "Start" then
		Module.StartTask1(Player)
	elseif StartOrStop == "Stop" then
		Module.StartTask1:Destroy()
	end
	print("QuestStarted")	
end)

here is a Module script

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local StartQuestRemote = ReplicatedStorage:WaitForChild("StartTheQuest")
local GiveMoneyRemote = ReplicatedStorage:WaitForChild("GiveMoney")
local StopQuestRemote = ReplicatedStorage:WaitForChild("StopTheQuest")
local Module = require(ServerStorage:WaitForChild("ModuleScript"))

StartQuestRemote.OnServerEvent:Connect(function(Player,StartOrStop)
	
	if StartOrStop == "Start" then
		Module.StartTask1(Player)
	elseif StartOrStop == "Stop" then
		Module.StartTask1:Destroy()
	end
	print("QuestStarted")	
end)

here is a Local script

local GiveMoneyButton = script.Parent.GiveMoneyButton
local StartQuestButton = script.Parent.StartQuestButton
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local GiveMoneyRemote = ReplicatedStorage:WaitForChild("GiveMoney")
local StartQuestRemote = ReplicatedStorage:WaitForChild("StartTheQuest")
local StopQuestButton = script.Parent.StopQuestButton

GiveMoneyButton.MouseButton1Click:Connect(function()
	
	GiveMoneyRemote:FireServer()
	
end)

StartQuestButton.MouseButton1Click:Connect(function()
	
	StartQuestRemote:FireServer("Start")
	
end)

StopQuestButton.MouseButton1Click:Connect(function()
	
	StartQuestRemote:FireServer("Stop")
	
end)
1 Like

Just use return at the end of the function.
Also if you want to disconnect a function do this

local Event
Event = Something.Something:Connect(function()
Event:Disconnect()
end)
1 Like

it said index to nil with ‘Disconnect’

StartQuestRemote.OnServerEvent:Connect(function(Player,StartOrStop)
	local PlayerMoney = Player:WaitForChild("leaderstats").Money
	local Task1Event
	if StartOrStop == "Start" then
		
		Task1Event = PlayerMoney.Changed:Connect(function()
			print(PlayerMoney.Value," Changed")
		end)
		
	elseif StartOrStop == "Stop" then
		Task1Event:Disconnect()
	end
	
end)

I would try to have the StartQuestRemote, the one that gets disconnected.
or

StartQuestRemote.OnServerEvent:Connect(function(Player,StartOrStop)
	local PlayerMoney = Player:WaitForChild("leaderstats").Money
	local Task1Event
	if StartOrStop == "Start" then
		
		local function PlayerMoney()
			print(PlayerMoney.Value," Changed")

		end
Task1Event = PlayerMoney.Changed:Connect(PlayerMoney)
elseif StartOrStop == "Stop" then
		return
	end
	
end)

I don’t really know if that will work, I feel like I’m missing something, if all else fails try to use return instead of Disconnect also I

i dont want the “Changed” event start until player StartOfStop == “Start”

The changed event is in the if statement so it can’t be started until StartOfStop == “Start”

you put the “Changed” event outside the if statement tho

i tried that , its not working , how do i use return instead of disconnect?

Refer to the solution on this post:

coroutine is not working

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")

local StartQuestRemote = ReplicatedStorage:WaitForChild("StartTheQuest")
local GiveMoneyRemote = ReplicatedStorage:WaitForChild("GiveMoney")
local StopQuestRemote = ReplicatedStorage:WaitForChild("StopTheQuest")

local StartTask1 = coroutine.create(function(Player)
	local PlayerMoney = Player:WaitForChild("leaderstats").Money
		PlayerMoney.Changed:Connect(function()
			print(PlayerMoney.Value," Changed")
		end)
	end)

StartQuestRemote.OnServerEvent:Connect(function(Player)
	
	coroutine.resume(StartTask1,Player)
	
end)

GiveMoneyRemote.OnServerEvent:Connect(function(Player)
	
	print(Player)
	local PlayerMoney = Player:WaitForChild("leaderstats").Money
	
	PlayerMoney.Value = PlayerMoney.Value + 100
	
end)

StopQuestRemote.OnServerEvent:Connect(function(Player)
	
	coroutine.yield(StartTask1,Player)
	
end)