Script Stops Working After Character Respawn

I have a script that rotates the character to where the mouse position is. The problem I ran into was that the script stops working after the character dies or respawns. I can’t seem to find a way to fix it.

local Camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()

repeat wait() until player and player.Character and player.Character.PrimaryPart
local character = player.Character
local primary = character.PrimaryPart

local bodyGyro ="BodyGyro")
bodyGyro.Parent = primary
bodyGyro.D = 40
bodyGyro.P = 10000
bodyGyro.MaxTorque =, 4000000, 4000000)

local function rayPlane(planepoint, planenormal, origin, direction)
	return -((origin-planepoint):Dot(planenormal))/(direction:Dot(planenormal))

while true do
	if character and primary then
		local ray = Camera:ScreenPointToRay(Mouse.X, Mouse.Y)
		local t = rayPlane(player.Character.Head.Position,, 1, 0), ray.Origin, ray.Direction)

		local primaryPos = primary.Position

		local plane_intersection_point = (ray.Direction * t) + ray.Origin
		bodyGyro.CFrame =,, primaryPos.Y, plane_intersection_point.Z))


Thanks so much if you could help :slight_smile:

Where is this script located??

The ideal place for it to be would be in StarterCharacterScripts

You can find it by opening up the StarterPlayer tab in the Explorer window.


You can add an update method for the character so that whenever the players character changes it will update the character variable…

Quick Example:

local PlayerService = game:GetService("Players") 

local Player = PlayerService.LocalPlayer --Player Object
local Character = Player.Character -- Update variable 

   Character = Player.Character
1 Like

Sorry for the late response. This script is located in start scripts

Which Starter folder? If the script is in StarterPlayerScripts, it will run only once when the player joins the game. If it is in StarterCharacterScripts, it will run every time the player’s character respawns.

Sorry for not clarifying, my bad. StarterPlayerScripts

Put it in StarterCharacterScripts and see if that fixes your problem.

Also, omit ItsSovxy’s script while you test that out. You shouldn’t need it if you place your original in CharacterScripts.

I’ll have to you remote functions for the mouse position now but that’s fine. I’ll see if I have any other problems

If your issue from this post has been solved, please mark the solution.