Module stops working when character respawns

local RunService = game:GetService("RunService")

local Player = game.Players.LocalPlayer
local character = Player.Character or Player.CharacterAdded:Wait()
local Camera = workspace.CurrentCamera

local Mouse = Player:GetMouse()

local Humanoid = character:WaitForChild("Humanoid")

local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
local Torso = character.Torso

local RootJoint = HumanoidRootPart.RootJoint
local LeftHipJoint = Torso["Left Hip"]
local RightHipJoint = Torso["Right Hip"]

local LeftHipJointC0 = LeftHipJoint.C0
local RightHipJointC0 = RightHipJoint.C0

local Neck = Torso:WaitForChild("Neck")
local Waist = HumanoidRootPart:WaitForChild("RootJoint")
local NeckOriginC0 = Neck.C0
local WaistOriginC0 = Waist.C0
Neck.MaxVelocity = 1/3

local connection

local HeadController = {}

function rotateHead(Character)
	local CameraCFrame = Camera.CoordinateFrame

	if Character:FindFirstChild("Torso") and Character:FindFirstChild("Head") then
		print("character and head")
		local TorsoLookVector = Torso.CFrame.lookVector
		local HeadPosition = Character.Head.CFrame.p

		if Neck and Waist then
			print("neck and waist")
			if Camera.CameraSubject:IsDescendantOf(Character) or Camera.CameraSubject:IsDescendantOf(Player) then
				local Point = Mouse.Hit.p
				print("camera has character")

				local Distance = (Character.Head.CFrame.p - Point).magnitude
				local Difference = Character.Head.CFrame.Y - Point.Y


				local goalNeckCFrame = CFrame.Angles(-(math.atan(Difference / Distance) * 0.5), (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 1, 0)
				Neck.C0 = Neck.C0:lerp(goalNeckCFrame * NeckOriginC0, 0.5 / 2).Rotation + NeckOriginC0.Position

				local xAxisWaistRotation = -(math.atan(Difference / Distance) * 0.5)
				local yAxisWaistRotation = (((HeadPosition - Point).Unit):Cross(TorsoLookVector)).Y * 0.5
				local rotationWaistCFrame = CFrame.Angles(xAxisWaistRotation, yAxisWaistRotation, 0)
				local goalWaistCFrame = rotationWaistCFrame*WaistOriginC0
				Waist.C0 = Waist.C0:lerp(goalWaistCFrame, 0.5 / 2).Rotation + WaistOriginC0.Position


				local currentLegCounterCFrame = Waist.C0*WaistOriginC0:Inverse()

				local legsCounterCFrame = currentLegCounterCFrame:Inverse()

				RightHipJoint.C0 =  legsCounterCFrame * RightHipJointC0
				LeftHipJoint.C0 = legsCounterCFrame * LeftHipJointC0
				print("bottom of function everything works")
			end
		end
	end	
end

connection = RunService.RenderStepped:Connect(function()
	if Player.Character and Player.Character.HumanoidRootPart then
		rotateHead(character)
	end
end)

Player.CharacterAdded:Connect(function(newCharacter)
	character = newCharacter

	if connection then 
		connection:Disconnect()
	end

	connection = RunService.RenderStepped:Connect(function()
		if Player.Character and Player.Character.HumanoidRootPart then
			rotateHead(character)
		end
	end)
end)

return HeadController

my function works perfectly fine up until the character is respawned. everything still prints but the logic in my function doesnt work. i copy and pasted the whole script into a local script and it worked perfectly fine but it wont work in a module script. im requiring it from this script below

local ReplicatedStorage = game:GetService("ReplicatedStorage")

local Controllers = ReplicatedStorage:WaitForChild("Controllers")

local player = game.Players.LocalPlayer

repeat task.wait(.1) until player.Character or player.CharacterAdded

local ControllerBlacklist = {
	
}

function LoadControllers(controllers)
	for _, controller in controllers:GetChildren() do
		if controller:IsA("ModuleScript") and not table.find(ControllerBlacklist, controller.Name) then
			local success, error = pcall(function()
				require(controller)
			end)

			if error then
				warn(error)
			end
		end
	end
end

LoadControllers(Controllers)

output shows no errors but everything in my function still prints and everything else seems to be working just fine except the logic doesnt go through

1 Like

Uh
How exactly are you running the modulescript? im confused.

1 Like

right here where i require(controller) the module is a “controller”

if thats not what you meant the local script where im doing that is inside of startercharacterscripts, ive tried also putting it in starterplayerscripts but nothing changed

1 Like

It probably needs to be run again after you reset, im pretty sure if the player isn’t being called again(or if any children of it) aren’t being called it won’t error.

1 Like

In your CharacterAdded event hander, you set character = newCharacter, but you forgot to update a few things: your variables Humanoid, HumanoidRootPart, Torso, RootJoint, etc… basically all of them at the top of the script reference the the first character you spawned, and never get updated when you respawn. All of those need to be updated to reference the new character’s parts every time you respawn, from within the CharacterAdded event handler function.

3 Likes

jeez i figured all of that would be fine and would get it all from the new character thank you

1 Like

Nope, any variables that stored references to the old character’s internal instances will not automatically get updated to point to the latest-spawned character, they will just become nil when the first-spawned character gets destroyed. Not much work for you though, pretty much just a copy-paste of all that code into your CharacterAdded function.

1 Like

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