How to disconnect an event from where you cannot access it

Trying to make it where you can disconnect a stepped event from outside of the function that it is residing in

I just cannot disconnect it, I tried to make a undefined variable then assigning it, but that did not work.

local function DashPropel(player,HRPVelocity,dashDirection,lengthOfDash)

	local dashSpeed
	local timeInDash = 0

	if dashDirection == "Front" then
		dashSpeed = 45
	elseif dashDirection == "Right" then
		dashSpeed = 45
	else
		dashSpeed = -45
	end

	local hitbox = AddDashHitbox(player,dashDirection)

	local SteppedDashService = RunService.Stepped:Connect(function()

		local camVector = DashUpdateFunction:invokeClient(player)

		if dashDirection == "Front" then
			dashSpeed -= 0.3
			timeInDash += 0.3
			player:SetAttribute("DashTime",timeInDash)

			HRPVelocity.VectorVelocity = Vector3.new(camVector.LookVector.X,0,camVector.LookVector.Z).unit * dashSpeed

		elseif dashDirection == "Back" then
			HRPVelocity.VectorVelocity = Vector3.new(camVector.LookVector.X,0,camVector.LookVector.Z).unit * dashSpeed
		else
			HRPVelocity.VectorVelocity = Vector3.new(camVector.RightVector.X,0,camVector.RightVector.Z).unit * dashSpeed
		end
	end)

	task.wait(lengthOfDash)

	SteppedDashService:Disconnect()
	
	if hitbox ~= nil then
		hitbox:Destroy()
	end

	player:SetAttribute("CurrentAction","Idle")

	RemoveLinearVelocity(HRPVelocity)

end
1 Like

I didn’t quite understand. Could you explain it in a slightly different way? Are you talking about “table,” for example?

1 Like

It looks like a scope issue, or maybe the variable isn’t being assigned fast enough before the task.wait() finishes.

Usually, the best way to handle this is to declare the variable as nil at the very top of your function (or even outside it if you need global access), and then assign the connection to it. That way, the script ‘knows’ the variable exists before it even tries to disconnect it.

Also, just a heads up: if lengthOfDash is very short, or if something errors inside the Stepped event, it might mess up the flow. Try pre-declaring it like this:

local SteppedDashService
-- then later...
SteppedDashService = RunService.Stepped:Connect(function() -- your code end)

This makes sure the variable is in the right scope. Let me know if that stops the error!"

2 Likes

no, there is no tables involved. the runservice.Stepped propels the user wherever they dashed, and it keeps on applying it so you can change the trajectory while dashing.
But I don’t want the stepped to be running constantly so normally it disconnects after a bit of time, but now I don’t know how to disconnect it from outside of the dashpropel function, so it can end early.

1 Like

When I try to do that, it does not detect whenever it is connected or not, and so it does not run.

DashModule.EndDash = function(player)

	local HRP = player.Character:FindFirstChild("HumanoidRootPart")
	local HRPVelocity = HRP:FindFirstChild("DashVelocity")
	
	if HRP == nil or HRPVelocity == nil then return end
	
	if SteppedDashService.Connected == true then -- never runs
		print("dash connected")	
	end
	SteppedDashService:Disconnect()
	
	RemoveLinearVelocity(HRPVelocity)
	
	local Hitbox = HRP:FindFirstChild("ForwardDash Hitbox")
	
	if Hitbox ~= nil then
		Hitbox:Destroy()
	end
	
end
1 Like

As far as I understand, you’re saying that everything after the “stepped” part doesn’t work, right? I’m asking to be sure.

1 Like

It works it, I just cannot disconnect outside of that, so I cannot end it early.

Like the EndDash I posted to the other person, that disconnect does not work nor does it show as connected.

My suggestion then is to group all dash functions by player, then add the functions to them. When you want to interrupt, you can close it directly from the player.

local DashTable = {}

local function EndDash(Player, hitbox, HRPVelocity)
	if DashTable[Player] then
		DashTable[Player]:Disconnect()
		DashTable[Player] = nil
	end

	if hitbox ~= nil then
		hitbox:Destroy()
	end

	Player:SetAttribute("CurrentAction","Idle")

	RemoveLinearVelocity(HRPVelocity)
end

local function DashPropel(player,HRPVelocity,dashDirection,lengthOfDash)

	local dashSpeed
	local timeInDash = 0

	if dashDirection == "Front" then
		dashSpeed = 45
	elseif dashDirection == "Right" then
		dashSpeed = 45
	else
		dashSpeed = -45
	end

	local hitbox = AddDashHitbox(player,dashDirection)

	DashTable[player] = RunService.Stepped:Connect(function()

		local camVector = DashUpdateFunction:invokeClient(player)

		if dashDirection == "Front" then
			dashSpeed -= 0.3
			timeInDash += 0.3
			player:SetAttribute("DashTime",timeInDash)

			HRPVelocity.VectorVelocity = Vector3.new(camVector.LookVector.X,0,camVector.LookVector.Z).unit * dashSpeed

		elseif dashDirection == "Back" then
			HRPVelocity.VectorVelocity = Vector3.new(camVector.LookVector.X,0,camVector.LookVector.Z).unit * dashSpeed
		else
			HRPVelocity.VectorVelocity = Vector3.new(camVector.RightVector.X,0,camVector.RightVector.Z).unit * dashSpeed
		end
	end)
	
	task.wait(lengthOfDash)
	
	EndDash(player, hitbox, HRPVelocity)
end
  1. use global table with players and their connections:

local PlayerDashConnections: { [Player]: RBXScriptConnection } = {}

  1. store connections

PlayerDashConnections[player] = SteppedDashService

  1. and disconnect when needed
if PlayerDashConnections[player] then
	PlayerDashConnections[player]:Disconnect()
	PlayerDashConnections[player] = nil
end
1 Like

Found out it was due to a timer prior to the module and not using global variable, thank you and all the others that helped me

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.