Can someone help me explaining this script

I have trouble understanding this script, please help me :pray:

--this script was originated by IAmDevForumMember, rewrote by 2112Jay
local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local LocalPlayer = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Root = LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local Mouse, Camera  = LocalPlayer:GetMouse(), Workspace.CurrentCamera
local angleX, x, y, tilt, vX, vY, sX, sY = 0, 0, 0, 0, 0, 0, 10, 10
local TouchEnabled = UserInputService.TouchEnabled
local randomX, randomY = nil, nil
local function lerp(v1, v2, t)
	return v1 + (v2 - v1) * t
end 

RunService.RenderStepped:Connect(function(dt)
	local velocity = Root.Velocity.magnitude
	if velocity > 0.02 then dt *= 60 randomX = math.random(1, 2) randomY = math.random(1, 2) --x(10,15), y(5,10)
		vX = dt <= 2 and lerp(vX, math.cos(tick() * 0.5 * randomX) * (math.random(5, 20) / 200) * dt, 0.05 * dt) or 0
		vY = dt <= 2 and lerp(vY, math.cos(tick() * 0.5 * randomY) * (math.random(2, 10) / 200) * dt, 0.05 * dt) or 0

		Camera.CFrame *= CFrame.Angles(0, 0, math.rad(angleX))
			* CFrame.Angles(math.rad(math.clamp(x * dt, -0.15, 0.15)), math.rad(math.clamp(y * dt, -0.5, 0.5)), tilt)
			* CFrame.Angles(math.rad(vX), math.rad(vY), math.rad(vY * 10))

		tilt = math.clamp(lerp(tilt, -Camera.CFrame:VectorToObjectSpace((Root and Root.Velocity or 
			Vector3.new()) / math.max(LocalPlayer.Character.Humanoid.WalkSpeed, 0.01)).X * 0.05, 0.1 * dt), -0.05, 0.05)

		if not TouchEnabled and dt < 2 then
			angleX = lerp(angleX, math.clamp(UserInputService:GetMouseDelta().X / dt * 0.15, -2.5, 2.5), 0.25 * dt)
		end x = lerp(x, math.sin(tick() * sX) / 5 * math.min(1, sY / 10), 0.25 * dt)
		y = velocity > 1 and lerp(y, math.cos(tick() * 0.5 * math.floor(sX)) * (sX / 200), 0.25 * dt) or lerp(y, 0, 0.05 * dt)
		sX, sY = velocity > 12 and 20 or (velocity > 0.1 and 12 or 0), velocity > 0.1 and 18 or (velocity > 0.1 and 14 or 0)
	end
end)

I suck at scripting btw

1 Like

If you’re asking what it is, it’s a camera shake script. I just stuck it in StarterCharacterScripts and tested it, and it actually works very well and it doesn’t error.

If you’re asking how it works, it seems to be getting the HumanoidRootPart, mouse, and camera, and using a lerp (Linear interpolation, basically a coded animation of sorts that involves linear functions) to bob the camera while the player is walking. I don’t know lerping very well, so I can’t easily explain that part, but the rest is much more simple.

local Workspace = game:GetService("Workspace")
local RunService = game:GetService("RunService")
local LocalPlayer = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local Root = LocalPlayer.Character:WaitForChild("HumanoidRootPart")
local Mouse, Camera  = LocalPlayer:GetMouse(), Workspace.CurrentCamera

^These lines are getting the main services needed for the upcoming function to work. The Workspace one is used to get the player related services/items, which is used to get the mouse and camera, etc. UserInputService is a service on it’s own, hence why it isn’t connected to the Workspace or anything other visible service. It controls things like keyboard and mouse inputs.

local angleX, x, y, tilt, vX, vY, sX, sY = 0, 0, 0, 0, 0, 0, 10, 10
local TouchEnabled = UserInputService.TouchEnabled
local randomX, randomY = nil, nil
local function lerp(v1, v2, t)
	return v1 + (v2 - v1) * t
end 

^I’m assuming that this bit is just the angles for the camera lerp (Again, I don’t know lerping), and it calls the function which is defined underneath it (The big block of equations and variables). I’m personally a bit unsure of what the mouse variables do, but again, I’m not good with lerping.

The defined lerp section uses CFrame (A part’s position and orientation in the 3D space) to shake the camera a bit. Sadly I can’t explain the specifics to the lerp, but here’s some DevForum posts I found that may help explain what it is:

Sorry that I couldn’t easily explain everything, but I hope this helps a bit! :]

(Edited for typos)

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.