Why does my script break after my character dies?

hello, i have a camera bobbing script that works initially, but when the character dies, it stops working and does very janky stuff with my character, not sure what’s causing it.

This is the code
while true do
	wait();
	if game.Players.LocalPlayer.Character then
		break;
	end;
end;
camera = game.Workspace.CurrentCamera;
character = game.Players.LocalPlayer.Character;
Z = 0;
damping = character.Humanoid.WalkSpeed / 2;
PI = 3.1415926;
tick = PI / 2;
running = false;
strafing = false;
character.Humanoid.Strafing:connect(function(p1)
	strafing = p1;
end);
character.Humanoid.Jumping:connect(function()
	running = false;
end);
character.Humanoid.Swimming:connect(function()
	running = false;
end);
character.Humanoid.Running:connect(function(p2)
	if p2 > 0.1 then
		running = true;
		return;
	end;
	running = false;
end);
character.Humanoid.Died:connect(function()
	running = false;
end);
function mix(p3, p4, p5)
	return p4 + (p3 - p4) * p5;
end;
while true do
	local step = game:GetService("RunService").RenderStepped:wait();
	fps = (camera.CoordinateFrame.p - character.Head.Position).Magnitude;
	if fps < 0.52 then
		Z = 0;
	else
		Z = 0;
	end;
	if running == true and strafing == false then
		tick = tick + character.Humanoid.WalkSpeed / 102 * (50*step);
	else
		if tick > 0 and tick < PI / 2 then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI / 2 < tick and tick < PI then
			tick = mix(tick, PI / 2, 0.9);
		end;
		if PI < tick and tick < PI * 1.5 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
		if PI * 1.5 < tick and tick < PI * 2 then
			tick = mix(tick, PI * 1.5, 0.9);
		end;
	end;
	if PI * 2 <= tick then
		tick = 0;
	end;
	camera.CoordinateFrame = camera.CoordinateFrame * CFrame.new(math.cos(tick) / damping, math.sin(tick * 2) / (damping * 2), Z) * CFrame.Angles(0, 0, math.sin(tick - PI * 1.5) / (damping * 20));
end;

any ideas as to why this happens?
help is appreciated, thank you in advance

2 Likes

where is the script parented?

also the issue looks more like LocalTransparencyModifer doesnt run on respawn rather than the bobbing

1 Like

the script is in startergui, the reason you can see the body is because i’m using CloneTrooper’s REALISM framework

I knew it.
scripts in StarterGui dont reset on character death
Parent it to StarterCharacterScripts

2 Likes

When you die a new character is made, so the original game.Players.LocalPlayer.Character will be nil.
You should be able to replace the old character with the new character by doing this:

local player = game.Players.LocalPlayer
local character = game.Players.LocalPlayer.Character

player.CharacterAdded:Connect(function(char)
    character = char
end)

Also, you will probably have to reset all the connections made with the old character

2 Likes

this broke the script, so it doesn’t bug out the camera anymore but the bobble doesn’t work after respawning

i should mention, the problem here is that at first the script works fine and the body doesn’t have a seizure, but when i die and respawn the body STARTS to have a seizure when i move.

that actually happens behind the scene too. I suggest making the root part always rotate the camera direction or mouse hit position

1 Like

how can i achieve this? what would i need to change in my code? or is it just not possible

u can do this

local player = game.Players.LocalPlayer
local char = player.Character or player.CharacterAdded:Wait()

game:GetService("RunService").RenderStepped:Connect(function()
    char:SetPrimaryPartCFrame(CFrame.LookAt(char.PrimaryPart.Position,char.PrimaryPart.Position + workspace.CurrentCamera.CFrame.LookVector * 5)
end)
1 Like

where do i replace it? not sure of where i should.

ok well i replaced this

local step = game:GetService("RunService").RenderStepped:wait();`

with

game:GetService("RunService").RenderStepped:Connect(function()
    char:SetPrimaryPartCFrame(CFrame.LookAt(char.PrimaryPart.Position,char.PrimaryPart.Position + workspace.CurrentCamera.CFrame.LookVector * 5)
end)

and it crashed roblox studio
awesome

u dont replace it u make different script for that (local one)
in the stater character scripts

i did
it also crashed my studio, thanks

Maybe change the RenderStepepd to Heartbeat

that made it worse and as i’m typing this my entire computer is having a stroke

oh shoot.
Thats so weird. RenderStepped shouldnt cause any crashes