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

Help! I tried to make 2 camera offset (FPP and TPP), and it's broken. How do i fix it?

So, I've been making a local script that shows your whole body in first person. I use a camera offset to adjust the position so you can see the body naturally and i wanted to make that camera offset visible only in First Person. So when get out of first person, My camera offset will be returned to normal. Here's the script

local player = game.Players.LocalPlayer local character = player.Character or player.CharacterAdded:wait() local human = character:WaitForChild("Humanoid")

local Player = game.Players.LocalPlayer

game:GetService("RunService").RenderStepped:connect(function() local c = game.Workspace:FindFirstChild(Player.Name) if c then c["Right Leg"].LocalTransparencyModifier = 0 c["Left Leg"].LocalTransparencyModifier = 0 c["Torso"].LocalTransparencyModifier = 0 end end) local function isFirstPerson() local camera = workspace.CurrentCamera if (character.Head.CFrame.p - camera.CFrame.p).magnitude < 1 then return true else return false end end

while wait() do local isFirstPerson = isFirstPerson()

if isFirstPerson == true then
	human.CameraOffset = Vector3.new(0, -0.25, -1.5)
end
if isFirstPerson == false then
	human.CameraOffset = Vector3.new(0, 0, 0)
end

end

When i test it, The camera offset is still normal until i zoom into first person. The camera offset rapidly changes from the first person only into the normal offset. And i can't fix it, Please help. And sorry because i can't explain the problem really well if you don't understand me.

1 comment

0

Local Script is placed in StarterGui

1 answer

oh, I made a third person and a first-person script myself, this might be of use for you!

EDIT: This script i not very beginner-friendly btw

local uis = game:GetService("UserInputService")
local rs = game:GetService("RunService")
local context = game:GetService("ContextActionService")

local plr = game:GetService("Players").LocalPlayer

local Camera = game:GetService("Workspace").CurrentCamera
local mouse = plr:GetMouse()
local cameraDB = false
local zoomDB = false

local xAngle = 0
local yAngle = 0
local Val = Instance.new("NumberValue", plr)
Val.Name = "CameraPosValue"
Val.Value = 8.5
local cameraPos = Vector3.new(2,0,Val.Value)

local is_tps = true

context:BindAction("CameraMovement", function(_,_,input)
	xAngle = xAngle - input.Delta.x*0.4
	yAngle = math.clamp(yAngle - input.Delta.y*0.4,-80,80)
end, false, Enum.UserInputType.MouseMovement)

-- secret undocumented technique, don't read
local recursive_transparency_mod
recursive_transparency_mod = function(obj, transparency)
	if obj:IsA("BasePart") then
		obj.LocalTransparencyModifier = transparency
	end
	
	for _, ch in pairs(obj:GetChildren()) do
		if ch:IsA("Tool") then
			continue
		end
		recursive_transparency_mod(ch, transparency)
	end
end

local function set_char_visibility(char, is_visible)
	local new_transparency = is_visible and 0 or 1
	
	recursive_transparency_mod(char, new_transparency)
end

plr.CharacterAdded:Connect(function(char)
	wait(0.1)
	set_char_visibility(char, is_tps)
	Camera.CameraType = Enum.CameraType.Scriptable
end)

rs.RenderStepped:Connect(function()
	local c = plr.Character
	-- no need for plr.CharacterAdded:Wait(), just skip
	if not c then
		return
	end
	
	local rootPart = c:FindFirstChild("HumanoidRootPart")
	if rootPart then
		local startCFrame = CFrame.new((rootPart.CFrame.p + Vector3.new(0,2,0)))*CFrame.Angles(0, math.rad(xAngle), 0)*CFrame.Angles(math.rad(yAngle), 0, 0)
		uis.MouseBehavior = Enum.MouseBehavior.LockCenter

		cameraPos = Vector3.new(2, 0, Val.Value)

		if is_tps then
			local cameraCFrame = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,cameraPos.Z))
			local cameraFocus = startCFrame + startCFrame:VectorToWorldSpace(Vector3.new(cameraPos.X,cameraPos.Y,-50000))

			Camera.CFrame = CFrame.new(cameraCFrame.p,cameraFocus.p)
		else
			Camera.CFrame = startCFrame
			rootPart.CFrame = CFrame.new(rootPart.CFrame.p) * CFrame.Angles(0,math.rad(xAngle),0)
		end
	end
end)

-- Camera Zoom --
local TweenService = game:GetService("TweenService")

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then	
		local Goal = {}
		Goal.Value = 2.5 --change for how much zoom do you want
		local Info = TweenInfo.new(0.30) 
		local Tween = TweenService:Create(Val, Info, Goal)
		Tween:Play()
	end
end)

uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then	
		local Goal = {}
		Goal.Value = 6.6
		local Info = TweenInfo.new(0.30) 
		local Tween = TweenService:Create(Val, Info, Goal)
		Tween:Play()
	end
end)

--FPP

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.Keyboard and input.KeyCode == Enum.KeyCode.V then --Change to whatever key you want
		is_tps = not is_tps
		
		if plr.Character then
			set_char_visibility(plr.Character, is_tps)
		end
	end
end)

its uhh quite long and complicated but if you wanna change the first-person toggle and such just go to the IserInputService function at the bottom and change the keycode and also has zoom (you can remove it if you want)

2 comments

0

Thank you man!

0

Hey man if you wanna help me out please accept my answer, it helps my rep and your rep and also no prob dude

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