Need help with connection signal not connecting second time

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

Bump 2x, still struggling with this.

1 Like

In your CharacterAdded event, you only update self.character if it is not already defined:

Players.LocalPlayer.CharacterAppearanceLoaded:Connect(function(character: Model) 
	if not self.character then -- << here
		self.character = character
	end
end)

Any line that references self.character will be referencing the first character model, regardless of how many times they have respawned. An example in your Start function:

local Humanoid = self.character:FindFirstChild("Humanoid") :: Humanoid

The humanoid defined here will be the humanoid of the first character model since self.character was not updated with the new one. You should just remove the conditional statement that checks if self.character is nil to fix this issue.

1 Like

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