I was trying to make a sliding module, and to stop sliding, I needed to disconnect a RBXScriptConnection. The problem is that when I run the Disconnect function for the connection, it doesn’t seem to do anything.
here’s the connection that is supposed to be disconnected
postmovementevt = RS.Heartbeat:Connect(function(dt)
timesincesstart += dt
local linervel = hrp.AssemblyLinearVelocity
hrp.AssemblyLinearVelocity = linervel - floornormal * linervel:Dot(floornormal)
ao.CFrame = CFrame.lookAt(Vector3.zero,hrp.AssemblyLinearVelocity)
if timesincesstart > 0.2 then
if math.abs((hrp.AssemblyLinearVelocity * Vector3.new(1,0,1)).magnitude) < 0.1 then
reset() -- supposed to disconnect the conncection
return
end
end
end)
and here’s is where it’s disconnected does
local function reset()
if onhit._connected then
onhit:Disconnect() -- seems to work just fine (i think)
end
if postmovementevt.Connected then
postmovementevt:Disconnect() -- doesn't seem to do anything
end
GF:stopAnimation("slideanimation",hum.Animator)
slidinghitbox:HitStop()
ao.Enabled = false
hum:ChangeState(Enum.HumanoidStateType.GettingUp)
end
Any help is appreciated. If more information is needed, please let me know.
function SlidingMod.Slide(player: Player,machine)
Rep.Remotes.Functions.AttachSlideHitbox:InvokeServer()
local slideanimation = game.ReplicatedStorage.Animations.Movement.slideanimation
local slideacc = 5
local slidinghitbox = playerhitboxen[player.Name]
local char = player.Character
local hrp:Part = char.HumanoidRootPart
local hum:Humanoid = char.Humanoid
local floornormal
local postmovementevt: RBXScriptConnection
local onhit
local ao: AlignOrientation = hrp:FindFirstChild("SlideOrientation")
local timesincesstart = 0
local function reset()
if onhit._connected then
onhit:Disconnect()
end
if postmovementevt.Connected then
postmovementevt:Disconnect()
end
GF:stopAnimation("slideanimation",hum.Animator)
slidinghitbox:HitStop()
ao.Enabled = false
hum:ChangeState(Enum.HumanoidStateType.GettingUp)
end
onhit = slidinghitbox.OnHit:Connect(function(part, _, result:RaycastResult)
slidinghitbox:HitStart()
floornormal = result.Normal
local groundslope:Vector3 = -char:WaitForChild("HumanoidRootPart").CFrame.RightVector:Cross(floornormal)
if groundslope.y >= 0.1 then
end
ao = hrp:FindFirstChild("SlideOrientation")
ao.Enabled = true
hum:ChangeState(Enum.HumanoidStateType.Physics)
GF:playAnimation(slideanimation,hum.Animator)
local forwardforce = groundslope.Unit * slideacc * hrp.AssemblyMass
hrp:ApplyImpulse(forwardforce)
postmovementevt = RS.Heartbeat:Connect(function(dt)
timesincesstart += dt
local linervel = hrp.AssemblyLinearVelocity
hrp.AssemblyLinearVelocity = linervel - floornormal * linervel:Dot(floornormal)
ao.CFrame = CFrame.lookAt(Vector3.zero,hrp.AssemblyLinearVelocity)
if timesincesstart > 0.2 then
if math.abs((hrp.AssemblyLinearVelocity * Vector3.new(1,0,1)).magnitude) < 0.1 then
reset()
return
end
end
end)
end)
slidinghitbox:HitStart()
end
Your onhit event is probably being fired multiple times which’ll overwrite the connection making you unable you to disconnect it, try add a debounce and see if that fixes it
Try checking to see if your connection is actually disconnecting.
if postmovementevt.Connected then
postmovementevt:Disconnect() -- doesn't seem to do anything
print(postmovementevt.Connected)
end
Side note: If you are seeing that there are multiple results (EX: You’re constantly seeing “true/false” being delivered to the output multiple times), then that means that @shadowmaster940 is most likely correct in their saying that the event is being fired multiple times. If you see “false”, that will mean that your connection is not connected. If you see true, however, then that means that your connection did not disconnect properly.
This works, sometimes. Only when I’m on flat ground. When going down slopes it no longer works.
debounce function:
local function debounce(numofframestowait)
canrun = false
coroutine.wrap(function()
for i = 1, numofframestowait do
RS.Heartbeat:Wait()
end
canrun = true
end)()
end
This is probably what is happening, though a debounce probably wouldn’t be the solution here, instead you should probably store a variable that states that you’re sliding and use that as a guard clause for the OnHit function.
local sliding = false
onhit = slidinghitbox.OnHit:Connect(function(part, _, result:RaycastResult)
if sliding then return end
sliding = true
...
Then inside the reset function, just simply set sliding to false. Alternatively, you could check if the connection is already connected to the postmovementevt variable and use that for the guard clause.
This does work, but doing this causes the event to only run once, and I need it to run multiple times so that things like the floor normal get updated.