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
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!"
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.
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
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