Hi,
In my movement class, when the player dies, it stops working, so I made a characteradded function that gets the character back after he dies. So now, how do I redirect the trove connections back without causing a memory leak?
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")
local PlayerSettings = require(ReplicatedStorage.PlayerSettings)
local Trove = require(ReplicatedStorage.Packages._Index["sleitnick_trove@1.1.0"]["trove"])
local Speedlines = require(ReplicatedStorage.Source.Classes.Speedlines)
-- acceleration
local moveDirectionDB = false
local BaseWaitTimer = PlayerSettings.BaseYield
local MinSpeed = PlayerSettings.AccelerationMinSpeed
local MaxSpeed = PlayerSettings.AccelerationMaxSpeed
local WaitTimer = BaseWaitTimer
local Movement = {}
Movement.__index = Movement
export type ClassType = typeof( setmetatable({} :: {
isMoving: boolean,
isRunning: boolean,
character: Model?,
Connections: Trove.ClassType,
}, Movement) )
function Movement.new(): ClassType
local self = {
isMoving = false,
isRunning = false,
character = nil,
Connections = Trove.new(),
};
setmetatable(self, Movement)
self:_init()
return self
end
function Movement._init(self: ClassType): ()
self:_setupCharacterAdded()
if self.character then
local Humanoid = self.character:FindFirstChild("Humanoid") :: Humanoid
local RootPart = self.character:FindFirstChild("HumanoidRootPart") :: BasePart
self.Connections:Connect(Humanoid:GetPropertyChangedSignal("MoveDirection"), function(...: any)
self:onMoveDirection(Humanoid, RootPart)
end)
Speedlines.new(self.character, 20, 1500):Start() -- speedlines
end
end
function Movement._setupCharacterAdded(self: ClassType): ()
self.Connections:Connect(Players.LocalPlayer.CharacterAdded, function(character: Model)
if not self.character then
self.character = character
end
end)
if Players.LocalPlayer then
if not self.character then
self.character = Players.LocalPlayer.Character:: Model
end
end
end
function Movement.onMoveDirection(self: ClassType, Humanoid, RootPart): ()
local velocity = RootPart.CFrame:Inverse() * (RootPart.Position + RootPart.AssemblyLinearVelocity)
local yDirection = math.atan2(velocity.X, -velocity.Z)
local roundedDirection = math.ceil(math.deg(yDirection) - 0.5)
local isStrafingOrBackwards = if roundedDirection >= 70 and roundedDirection <= 100 or
roundedDirection <= -70 and roundedDirection >= -100 or
roundedDirection <= -135 or roundedDirection >= 135
then true else false
if Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and not isStrafingOrBackwards and moveDirectionDB == false and Humanoid.WalkSpeed < MaxSpeed then
moveDirectionDB = true
while Humanoid.MoveDirection ~= Vector3.new(0, 0, 0) and Humanoid.WalkSpeed < MaxSpeed do
Humanoid.WalkSpeed = Humanoid.WalkSpeed + 1
task.wait(WaitTimer)
WaitTimer = WaitTimer / 1.1
end
moveDirectionDB = false
elseif Humanoid.MoveDirection == Vector3.new(0, 0, 0) or isStrafingOrBackwards then
WaitTimer = BaseWaitTimer
Humanoid.WalkSpeed = MinSpeed
end
end
function Movement.Destroy(self: ClassType): ()
self.Connections:Destroy()
end
return Movement