Characters Y velocity stays at 0 when player resets

Hi,

I made a camera class with land shake and everything is working well but when I reset, for some reason the vertical velocity of the player repeats 0. I have no idea why this is happening.

--!strict

--/ services
local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local UserInputService = game:GetService("UserInputService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
--/ directories
local Source = ReplicatedStorage.Source
local Modules = Source.Dependencies
local Classes = Source.Classes
local Packages = ReplicatedStorage.Packages
--/ requires
local Trove = require(Packages._Index["sleitnick_trove@1.1.0"]["trove"])
local CameraShaker = require(Modules.CameraShaker)
local CameraShakeInstance = require(Modules.CameraShaker.CameraShakeInstance)
--/ variables
local CurrentCamera = workspace.CurrentCamera
--/ class
local Camera = {}
Camera.__index = Camera

export type ClassType = typeof( setmetatable({} :: {
	Connections: Trove.ClassType,

	character: any,
}, Camera) )

local camShake = CameraShaker.new(Enum.RenderPriority.Camera.Value, function(shakeCf)
	CurrentCamera.CFrame = CurrentCamera.CFrame * shakeCf
end)

function Camera.new(): ClassType
	local self = {
		Connections = Trove.new(),

		character = nil,
	};

	setmetatable(self, Camera)

	self:_init()

	return self
end

--/ private methods
function Camera._init(self: ClassType): () -- initilize
	Players.LocalPlayer.CameraMode = Enum.CameraMode.LockFirstPerson
	camShake:Start()
	
	self:_setupCharacterAddedFunction()
end

function Camera._setupCharacterAddedFunction(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

--/ update the camera (runservice)
function Camera.Update(self: ClassType, deltaTime: number): ()	
	if self.character then
		local Root = self.character:FindFirstChild("HumanoidRootPart") :: BasePart
		local Humanoid = self.character:FindFirstChild("Humanoid") :: Humanoid
		local MouseDelta = UserInputService:GetMouseDelta()
		
		local VerticalSpeed = math.abs(Root.AssemblyLinearVelocity.Y)
		
		-- Land shake:
		local isGrounded = if Humanoid:GetState() == Enum.HumanoidStateType.Landed
			  then true else false
		
		print(VerticalSpeed)
		if VerticalSpeed > 65 and isGrounded then
			camShake:ShakeOnce(1.5, 3.5, 0.1, 0.575)
		end
	end
end

function Camera.Enable(self: ClassType): ()
	local function _update(deltaTime)
		self:Update(deltaTime)
	end

	self.Connections:BindToRenderStep("Camera", Enum.RenderPriority.Camera.Value + 1, _update)
end

function Camera.Disable(self: ClassType): ()
	self.Connections:Clean()
end

return Camera

After I reset, the vertical speed / assemblylinearvelocity.y prints this:

 0 (x569)
1 Like

I still need help in this issue…

Does the issue not lie in this logic?

if not self.character then
	self.character = character
end

Your character key never updates, referencing the old character.

1 Like

Wait how am I referencing the same character if i’m using a characteradded to update the self.character? I also checked that if it was working and i’m sure it is.

When your CharacterAdded gets fired, you check if there isn’t a character on self.character. If not, you set self.character to the new character. However, the logic fails because the self.character still exists in memory and is truthy, failing the conditional check. Remove the if condition completely.

The reason why it works the first time because you have the same logic below, but this time you don’t have a self.character initially, passing the condition.

Dude I forgot I had this same problem and I fixed it a long time ago thats why I didn’t think anything of it. Thanks for picking that up lol…

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