Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How Do I Automatically Run This Script After The Character Respawns?

So I have a shoulder camera script on my game and i have a issue which is whenever the player resets their character the camera stays on the last position that the camera was in before the player reset i can't really find a way to fix this issue so i'm asking for help here is my local script.

local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local RunService = game:GetService("RunService")
 
local camera = workspace.CurrentCamera
local cameraOffset = Vector3.new(2, 2, 8)
local player = Players.LocalPlayer
local FreeToggle = true

player.CharacterAdded:Connect(function(character)
	local humanoid = character:WaitForChild("Humanoid")
	local rootPart = character:WaitForChild("HumanoidRootPart")
	local t = character:WaitForChild("Torso")
	humanoid.AutoRotate = false
 
	local cameraAngleX = 0
	local cameraAngleY = 0
 
	local function playerInput(actionName, inputState, inputObject)
		-- Calculate camera/player rotation on input change
		if inputState == Enum.UserInputState.Change then
			cameraAngleX = cameraAngleX - inputObject.Delta.X
			-- Reduce vertical mouse/touch sensitivity and clamp vertical axis
			cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
			-- Rotate root part CFrame by X delta
			rootPart.CFrame = rootPart.CFrame * CFrame.Angles(0, math.rad(-inputObject.Delta.X), 0)
		end
	end
	ContextActionService:BindAction("PlayerInput", playerInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
 
	RunService.RenderStepped:Connect(function()
		if camera.CameraType ~= Enum.CameraType.Scriptable then
			camera.CameraType = Enum.CameraType.Scriptable
		end
		local startCFrame = CFrame.new((rootPart.CFrame.Position)) * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
		local cameraCFrame = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, cameraOffset.Z))
		local cameraFocus = startCFrame:ToWorldSpace(CFrame.new(cameraOffset.X, cameraOffset.Y, -10000))
		camera.CFrame = CFrame.new(cameraCFrame.Position, cameraFocus.Position)	
	end)
	
	local dir = camera.CoordinateFrame.lookVector * Vector3.new(1,0,1)
        local b = Instance.new("BodyGyro",t)
        b.Name = "ShoulderCamGyro"
        b.maxTorque = Vector3.new(999999,999999,999999)
        b.cframe = CFrame.new(t.Position,t.Position+(camera.CoordinateFrame.lookVector*Vector3.new(1,0,1)))
        b.D = 100
        game:GetService("RunService").RenderStepped:connect(function()
            if b and t then
                local dir = camera.CoordinateFrame.lookVector * Vector3.new(1,0,1)
                b.cframe = CFrame.new(t.Position,t.Position+dir)
	end
end)

end)

 
local function focusControl(actionName, inputState, inputObject)
	-- Lock and hide mouse icon on input began
	if inputState == Enum.UserInputState.Begin then
		local mouse = game.Players.LocalPlayer:GetMouse()
		UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
		UserInputService.MouseIconEnabled = true
		mouse.Icon = "rbxassetid://4918750366"
		ContextActionService:UnbindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)
	end
end

UserInputService.InputBegan:Connect(function(input, gameProcessedEvent)
	local controls = require(game:GetService("Players").LocalPlayer.PlayerScripts.PlayerModule):GetControls()
	if input.KeyCode == Enum.KeyCode.M then
		if FreeToggle == true then
			FreeToggle = false
			UserInputService.MouseBehavior = Enum.MouseBehavior.Default
			controls:Disable()
		else
			FreeToggle = true
			UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
			controls:Enable()
		end
	end
end)	

ContextActionService:BindAction("FocusControl", focusControl, false, Enum.UserInputType.MouseButton1, Enum.UserInputType.Touch, Enum.UserInputType.Focus)

Thanks In Advance! :D

1 answer

You should try this!

Player.CharacterAdded:Connect(function()

At the very start, what it does is that everytime the character is added/reloaded via death or reset it runs the script also at the very bottom add end)

Hope this helped!

2 comments

0

This Did Not Work Sadly :(

0

Huh, strange, lemme see

This discussion is preserved as it was. New answers, comments, and votes are closed.