Player Leaning Script Breaks Whenever the Player Dies

Hello, I’m trying to make a lean script that leans the players camera towards the left and right with the press of Q and E on the players keyboard.

However the script keeps breaking once the player dies or resets and I couldn’t exactly find solutions to this or if something like this has been already posted before and if there is just let me know. I have also tried to figure out what the issue is and it seems to be related to the tweenservice? But I haven’t exactly worked with that till now, so I don’t really know what causes it to stop working once the player dies.

The code should be placed in a local script in StarterPlayerScripts
Code:

wait(1.5) --this is so the script doesnt break when the player is loading into the game

local Player = game.Players.LocalPlayer
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
local Camera = game.Workspace.CurrentCamera
local Humanoid = Player.Character.Humanoid
local RollFunctionRight
local RollFunctionLeft
local tweenService = game:GetService("TweenService")

local gilbert = TweenInfo.new(0.65, Enum.EasingStyle.Sine)
local hubert = TweenInfo.new(0.35, Enum.EasingStyle.Sine)
-------------------------------------------------------------------------------------------------------------
function BeginPeekRight(input)
	if input.KeyCode == Enum.KeyCode.E then
		--Player.Character.LocalScript.Enabled=false --bobbing script is disabled
		tweenService:Create(Humanoid, gilbert, {CameraOffset = Vector3.new(2.85, 0.5, 0)} ):Play()
		RollFunctionRight = RunService.RenderStepped:Connect(function(dt)
			Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(-20))
			Player.Character.Torso.Anchored=true --this is to set the player in place
		end)
	end
end

function StopPeekRight(input)
	if input.KeyCode == Enum.KeyCode.E then
		RollFunctionRight:Disconnect()
		tweenService:Create(Humanoid, hubert, {CameraOffset = Vector3.new(0, 0, 0)} ):Play()
		Player.Character.Torso.Anchored=false --player can now move again
		--Player.Character.LocalScript.Enabled=true --enables bobbing script
	end
end

function BeginPeekLeft(input)
	if input.KeyCode == Enum.KeyCode.Q then
		--Player.Character.LocalScript.Enabled=false
		tweenService:Create(Humanoid, gilbert, {CameraOffset = Vector3.new(-2.85, 0.5, 0)} ):Play()
		RollFunctionLeft = RunService.RenderStepped:Connect(function(dt)
			Camera.CFrame = Camera.CFrame * CFrame.Angles(0, 0, math.rad(20))
			Player.Character.Torso.Anchored=true
		end)
	end
end

function StopPeekLeft(input)
	if input.KeyCode == Enum.KeyCode.Q then
		tweenService:Create(Humanoid, hubert, {CameraOffset = Vector3.new(0, 0, 0)} ):Play()
		RollFunctionLeft:Disconnect()
		Player.Character.Torso.Anchored=false
		--Player.Character.LocalScript.Enabled=true
	end
end
UserInputService.InputBegan:Connect(BeginPeekRight)
UserInputService.InputEnded:Connect(StopPeekRight)
UserInputService.InputBegan:Connect(BeginPeekLeft)
UserInputService.InputEnded:Connect(StopPeekLeft)

I’m just trying to find a fix as to why it keeps breaking whenever the player dies, I know the script could be improved and simplified here but as of now that is my main focus.

Try placing it in StarterCharacterScripts instead. See what results you get.

1 Like

I’m not exactly sure why I didn’t think of this at the time, but it appears to have solved the issue so thank you.

1 Like

As a little sidenote, even though putting the script in startercharacterscripts completely eradicates this issue, but instead of doing

wait(1.5)

to wait for the game to load (task.wait() is better btw).
you should be doing this

if not game:IsLoaded() then
  game.Loaded:Wait()
end

to be completely certain the game loaded for the player no matter the loading time for the player

1 Like

that check won’t matter i had localscripts in replicatedfirst have access to the localplayer without any yielding, maybe i tested it wrong tho, but as far as i know that game:IsLoaded() method should only be used by exploiters or server scripts

That’s because the scripts in replicatedfirst … get replicated first ? So that they reference the player as fast as possible. Also the game.IsLoaded check is handy for accessing game asset’s you know will not replicate immediately. Second, game.Isloaded is used primarily for localscripts over serverscripts since everything will be readily available to the server at runtime.