Hi,
I know the title is weird but it’s kind of hard to explain without showing the problem.
I have a movement class that is supposed to work when the player dies. I added a characteradded connection and that works, then I cleaned the connections (using trove) and I tried to reconnect the move direction connection and it doesn’t work at all. Can anyone tell me what is happening?
--!strict
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local UserInputService = game:GetService("UserInputService")
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,
isEnabled: boolean,
character: Model?,
speedlines: Speedlines.ClassType?,
Connections: Trove.ClassType,
}, Movement) )
function Movement.new(): ClassType
local self = {
isMoving = false,
isRunning = false,
isEnabled = false,
character = nil,
speedlines = nil,
Connections = Trove.new(),
};
setmetatable(self, Movement)
self:_init()
return self
end
function Movement._init(self: ClassType): ()
self:_setupCharacterAdded()
end
function Movement._setupCharacterAdded(self: ClassType): ()
Players.LocalPlayer.CharacterAppearanceLoaded:Connect(function(character: Model)
if not self.character then
self.character = character
end
if self.isEnabled then
print("hi")
self:Stop()
self:Start()
end
end)
if Players.LocalPlayer then
if not self.character then
self.character = Players.LocalPlayer.Character:: Model
end
end
end
function Movement.Start(self: ClassType): ()
if self.character then
print("passed 1x")
local Humanoid = self.character:FindFirstChild("Humanoid") :: Humanoid
local RootPart = self.character:FindFirstChild("HumanoidRootPart") :: BasePart
print("passed 2x")
self.Connections:Connect(Humanoid:GetPropertyChangedSignal("MoveDirection"), function(...: any)
print("move direction")
self:onMoveDirection(Humanoid, RootPart)
end)
self.isEnabled = true
end
end
function Movement.Stop(self: ClassType): ()
self.Connections:Clean()
self.isEnabled = false
if self.character then
local Humanoid = self.character:FindFirstChild("Humanoid") :: Humanoid
Humanoid.WalkSpeed = PlayerSettings.CharacterWalkSpeed
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
Output:
21:35:20.330 passed 1x - Client - Movement:76
21:35:20.330 passed 2x - Client - Movement:80
21:35:21.030 ▶ move direction (x48) - Client - Movement:82
21:35:24.096 ▶ move direction (x39) - Client - Movement:82
-- prints hi during character added connection as shown below.
21:35:26.230 hi - Client - Movement:60
21:35:26.230 passed 1x - Client - Movement:76
21:35:26.230 passed 2x - Client - Movement:80
As you can see from the Output, it no longer prints movedirection once I die, but it does pass through