Custom shiftlock adds onto it's own offset when character changes

I have a script for mobile players that gives them a custom shiftlock that makes the camera go up their head a bit and aim at the center thru an offset, but in my game’s character changing systems, it adds onto the offset and slowly rises up infinitely

When the character dies, and respawns the camera goes back into the intended position/look
So it has to be related to the character changing systems or it just adds onto the previous camera position leading to the addition with the offset

Character changing script:

Camera script, Im using a script from the devforum to make the custom shiftlock, i don’t think i have the link:

Running camera script and disconnecting it when player dies:

Offset value:
image

Logically the camera should reset with each character change because im destroying the character, when the character dies, the player is sent into a box without the “Playing” tag and when they press play, their custom character is loaded with the tag; So the character creation should reset it

Recreation of bug on PC since I don’t have devofrum on mobile
Default:

After changing character:

Why are you using the camera? use Humanoid.CameraOffset

Unless you have logic elsewhere to check when a player dies, this will not properly unbind the function, so the onStep() function will keep running and begin stacking every time the player spawns a new character. Instead of (0, 1.5, 0) being added to the offset every frame, it becomes (0, 3, 0) and so on.

This is because this block of code, connected to the CharacterAdded signal, only runs once - when the character first spawns. In order to detect when this character has been removed during respawn, you can use AncestryChanged to trigger the UnbindFromRenderStep().

Example:

game:GetService("Players").LocalPlayer.CharacterAdded:Connect(function(char)
	if char:HasTag("Playing") then
		RunService:BindToRenderStep("Mobile/ConsoleShiftLock",Enum.RenderPriority.Camera.Value,OnStep)
	end
	
	local characterRemovedConnection: RBXScriptConnection
	characterRemovedConnection = char.AncestryChanged:Connect(function(_, newParent)
		if newParent == nil then -- the character has been removed and no longer has a parent.
			characterRemovedConnection:Disconnect() -- dispose of this connection variable
			characterRemovedConnection = nil
			
			-- and unbind the function!
			RunService:UnbindFromRenderStep("Mobile/ConsoleShiftLock")
		end
	end)
end)
1 Like

Thanks for explaining the concept; I thought CharacterAdded would keep listening like other connections, your script also was the solution

1 Like

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