Review my magnitude system

--MODULE SHAKE CAMERA

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid:: Humanoid

local canPlay = true
--libs
local VECTOR_3 = Vector3.new
local MATH_RANDOM = math.random

local ShakeCamera = {}

function ShakeCamera:playShake(shakeIntensity: number, shakeSpeed: number): ()
	if not canPlay then return end
	
	local shakeOffset = Vector3.new(
		MATH_RANDOM(-shakeIntensity, shakeIntensity),
		MATH_RANDOM(-shakeIntensity, shakeIntensity),
		MATH_RANDOM(-shakeIntensity, shakeIntensity)
	)
	
	local originalOffset = VECTOR_3(0, 0, 0)
	
	humanoid.CameraOffset = originalOffset + shakeOffset
	
	canPlay = false
	
	task.wait(shakeSpeed)
	
	canPlay = true
	
	humanoid.CameraOffset = VECTOR_3(0, 0, 0)
end

return ShakeCamera

--script
if not game:IsLoaded() then game.Loaded:Wait() end
--SERVICE
local RunService = game:GetService("RunService")
local ReplicatedStorage = game.ReplicatedStorage
local Lighting = game:GetService("Lighting")
--MODULES
local ShakeCamera = require(ReplicatedStorage.Camera.ShakeCamera)
--PLAYER
local player = game.Players.LocalPlayer
local character = player.Character
local humanoidRootPart = character.HumanoidRootPart:: BasePart
local humanoid = character.Humanoid:: Humanoid
--LIGHTING
local colorCorrection = Lighting.ColorCorrection
--WORKSCPACE
local Build = workspace:WaitForChild("Build")
local secretPart = Build:FindFirstChild("SecretPart")
--NOT CHANGE VALUE
local MIN_DISTANCE = 50
local MAX_DISTANCE = 10
local MAX_BRIGHTNESS = -0.8
local MAX_CONTRAST = 1
local MAX_SHAKE_SPEED = 0.3
local MAX_SHAKE_INTENSITY = 1.5
--Libs
local COLOR_FROM_RGB = Color3.fromRGB
--CONNECTION
local heatbeatConnection: RBXScriptConnection

local function resetEffects()
	colorCorrection.Brightness = 0
	colorCorrection.Contrast = 0
	colorCorrection.TintColor = COLOR_FROM_RGB(255, 255, 255)
end

local function setEffects(goal: number)
	local brightness_goal = goal * MAX_BRIGHTNESS
	local contrast_goal = goal * MAX_CONTRAST
	local color_goal = 255 * (1 - goal)
	local shake_intensity_goal = goal * MAX_SHAKE_INTENSITY
	local shake_speed_goal = goal * MAX_SHAKE_SPEED
	
	colorCorrection.Brightness = brightness_goal
	colorCorrection.Contrast = contrast_goal
	colorCorrection.TintColor = COLOR_FROM_RGBB(255, color_goal, color_goal)
	
	ShakeCamera:playShake(shake_intensity_goal, shake_speed_goal
		
	)
end

local function actualizeMagnitude(distance: number)
	if distance <= MIN_DISTANCE then
		local goal = 1 - (distance / MIN_DISTANCE)
		setEffects(goal)
	end
	
	--if distance <= MAX_DISTANCE then
	--	heatbeatConnection:Disconnect()
	--	humanoidRootPart.Anchored = true
	--	humanoid:Move(humanoidRootPart.po)
	--end
end

local function onHeartbeat(dt: number)
	if not character.Parent then
		heatbeatConnection:Disconnect()
		return
	end
	
	local distance = (humanoidRootPart.Position - secretPart.Position).Magnitude
	
	actualizeMagnitude(distance)
end

heatbeatConnection = RunService.Heartbeat:Connect(onHeartbeat)
2 Likes