I've answered this question many times.
wait(1)
ALLOW_TOGGLE = true
TOGGLE_KEY = "p"
DEFAULT_ENABLED = true
SMOOTH_ROTATION = true
player = game.Players.LocalPlayer
mouse = player:GetMouse()
enabled = DEFAULT_ENABLED
c = workspace.CurrentCamera
mirror = {}
h = nil
userInput = game:GetService("UserInputService")
isMobile = false
function createModel()
local m = Instance.new("Model",c)
m.Name = "ViewModel"
local hack = Instance.new("Part",m)
hack.Name = "HumanoidRootPart"
hack.Transparency = 1
hack.CanCollide = false
hack.Anchored = true
hack.CFrame = CFrame.new()
local h = Instance.new("Humanoid",m)
h.Sit = true
h.Changed:connect(function ()
h.Sit = true
end)
return m
end
function registerPart(v)
local function mirrorPart(p,t)
local m = p:clone()
m.CanCollide = false
m.Parent = model
m.Transparency = (t or 0)
local w = Instance.new("Weld",m)
w.Part0 = p
w.Part1 = m
mirror[v] = m
if m.Name == "Head" then
m.Name = "H"
end
v.Changed:connect(function (property)
local blist = {Position = true, CFrame = true, Parent = true, CanCollide = true}
if not blist[property] then
pcall(function ()
m[property] = v[property]
end)
end
end)
v.ChildAdded:connect(function (ch)
ch.Archivable = true
local cl = ch:clone()
cl.Parent = m
mirror[ch] = cl
end)
v.ChildRemoved:connect(function (ch)
if mirror[ch] then
mirror[ch]:Destroy()
mirror[ch] = nil
end
end)
m.Changed:connect(function ()
m.CanCollide = false
end)
end
if v:IsA("BasePart") and not mirror[v] and v.Name ~= "HumanoidRootPart" then
mirrorPart(v)
elseif v:IsA("Hat") then
local h = v:WaitForChild("Handle")
mirrorPart(h)
elseif v:IsA("Clothing") or v:IsA("CharacterAppearance") then
v:clone().Parent = model
end
end
function updateCam()
if h then
if enabled then
if model then
local range = model:GetExtentsSize().Y/2
local y = c.CoordinateFrame.lookVector.Y
h.CameraOffset = Vector3.new(math.max(1.5,1.5-y),math.abs(y/2)-y*3.8642337322235,4-math.abs(y))
else
h.CameraOffset = Vector3.new(1.5,0.5,4)
end
else
h.CameraOffset = Vector3.new()
end
end
end
function newCharacter(char)
if not char then return end
mirror = {}
for _,v in pairs(c:GetChildren()) do
if v.Name == "ViewModel" then
v:Destroy()
end
end
if not enabled then return end
model = createModel()
for _,o in pairs(char:GetChildren()) do
registerPart(o)
end
char.ChildAdded:connect(registerPart)
h = nil
while not h do
for _,v in pairs(char:GetChildren()) do
if v:IsA("Humanoid") then
h = v
break
end
end
wait()
end
updateCam()
player.CameraMaxZoomDistance = 0.5
h.Died:connect(function ()
if model then
model:Destroy()
end
end)
local t = char:WaitForChild("Torso")
for _,v in pairs(t:GetChildren()) do
if v.Name == "ShoulderCamGyro" then
v:Destroy()
end
end
if SMOOTH_ROTATION and enabled and mouse then
local dir = c.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+(c.CoordinateFrame.lookVector*Vector3.new(1,0,1)))
b.D = 100
game:GetService("RunService").RenderStepped:connect(function ()
if h then
h.AutoRotate = not enabled
end
if b and t then
local dir = c.CoordinateFrame.lookVector * Vector3.new(1,0,1)
b.cframe = CFrame.new(t.Position,t.Position+dir)
end
end)
end
end
if mouse and ALLOW_TOGGLE and #TOGGLE_KEY == 1 and type(TOGGLE_KEY) == "string" then
mouse.KeyDown:connect(function (key)
if string.lower(key) == TOGGLE_KEY then
enabled = not enabled
if model then
model:Destroy()
newCharacter(player.Character)
updateCam()
end
end
end)
mouse.Move:connect(updateCam)
end
if player.Character then
newCharacter(player.Character)
end
local con do
con = game:GetService("RunService").RenderStepped:connect(function ()
isMobile = userInput.TouchEnabled
if isMobile then
con:disconnect()
SMOOTH_ROTATION = false
newCharacter(player.Character)
end
end)
end
2 comments
Please edit your post to use the "Lua" formatting button. That way, we can then read your code a bit easier.
Ok, there you go.