How do I make a screenshake that decreases in intensity gradually?

hey guys, as the title says, how would I achieve this?
this is what I’m trying to achieve:

this is what I originally wrote: (this errors actually)

function camStuff.GradualScreenshake(
    humanoid: Humanoid,
    duration: number,
    intensity: number
)
    local startTime = tick()
    local value = nil
    
    intensity *= 100
    value = math.floor(intensity / (duration * 10) )
    
    repeat
        task.wait()
        local endTime = tick()

        local xoffset = math.random(-intensity, intensity) / 500
        local yoffset = math.random(-intensity, intensity) / 500
        local zoffset = math.random(-intensity, intensity) / 500

        humanoid.CameraOffset = Vector3.new(xoffset, yoffset, zoffset)

        intensity = math.ceil(intensity * 0.5)
        
    until endTime - startTime >= duration

    humanoid.CameraOffset = Vector3.new(0, 0, 0)
end

this is what my friend wrote for me

Friend's code
local curve = Instance.new("FloatCurve")

curve:InsertKey(
    FloatCurveKey.new(0, 0, Enum.KeyInterpolationMode.Cubic)
)

curve:InsertKey(
    FloatCurveKey.new(0.1, 1, Enum.KeyInterpolationMode.Cubic)
)

curve:InsertKey(
    FloatCurveKey.new(0.75, 0.1, Enum.KeyInterpolationMode.Cubic)
)

curve:InsertKey(
    FloatCurveKey.new(1, 0, Enum.KeyInterpolationMode.Cubic)
)

local RunService = game:GetService("RunService")

function camStuff.GradualScreenshake(
    humanoid: Humanoid,
    duration: number,
    intensity: number?
)
    local timer = 0
    
    local targetCameraOffset = Vector3.zero
    
    local bind = RunService.RenderStepped:Connect(function(dt)
        humanoid.CameraOffset = humanoid.CameraOffset:Lerp(targetCameraOffset, math.clamp(dt*32, 0, 1))
    end)
    
    repeat
        local scale = math.clamp(timer / duration, 0, 1)
        -- 0 to 1
        
        local curvedIntensity = curve:GetValueAtTime(scale)
        
        local dt = task.wait(1/60)
        timer += dt
        
        local evaulated = timer * curvedIntensity
        
        local offset = Vector3.new(
            (math.random() - 0.5) * 2,
            (math.random() - 0.5) * 2,
            (math.random() - 0.5) * 2
        ) * curvedIntensity * (intensity or 1)
        targetCameraOffset = offset
    until timer >= duration+0.1
    bind:Disconnect()
    RunService.RenderStepped:Wait()
    humanoid.CameraOffset = Vector3.zero
end

but just doesn’t yield the results I want, could anybody give me tips on how to achieve the one shown in the video I linked? thanks so much! :coefficients:

couldnt you just use a tween?

()

yeah, thats what i thought.

For the post’s owner:Find a property that controls player camera positioning (probably CFrames but i dont think it would be a good camera shake if u used CFrames) and tween it with two very short tweens, one that shakes to the left and one that shakes to the right

or you can just randomize it to make a random screen shake

how about this, would this work? (also, is it recommended to use tick()?)

function camStuff.GradualScreenshake(
    humanoid: Humanoid,
    duration: number,
    intensity: number
)
    local startTime = tick()
    local numValue = Instance.new("NumberValue")
    numValue.Value = intensity * 100
    intensity = numValue.Value

    local info = TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
    local tween = tweenService:Create(numValue, info, {Value = 1})
    tween:Play()
    
    repeat
        task.wait()
        local endTime = tick() 

        --will this error cuz ur putting a decimal inside math.random?
        local xoffset = math.random(-intensity, intensity) / 500
        local yoffset = math.random(-intensity, intensity) / 500
        local zoffset = math.random(-intensity, intensity) / 500 

        humanoid.CameraOffset = Vector3.new(xoffset, yoffset, zoffset) 
         
    until endTime - startTime >= duration

    humanoid.CameraOffset = Vector3.new(0, 0, 0)
end

A. you can put decimals in math.random()
B. this wont gradually decrease since intensity is constant and 500 is too.
What I would do:

function camStuff.GradualScreenshake(
    humanoid: Humanoid,
    duration: number,
    intensity: number
)
    local startTime = tick()
    local numValue = Instance.new("NumberValue")
    numValue.Value = intensity * 100
    intensity = numValue.Value

    local info = TweenInfo.new(duration, Enum.EasingStyle.Linear, Enum.EasingDirection.In, 0, false, 0)
    local tween = tweenService:Create(numValue, info, {Value = 1})
    tween:Play()
    local timeStamp = 0
    

    repeat
        task.wait()
        local timeStamp += tick()
        --will this error cuz ur putting a decimal inside math.random?
        local xoffset = math.random(-intensity, intensity)
        local yoffset = math.random(-intensity, intensity)
        local zoffset = math.random(-intensity, intensity)

        humanoid.CameraOffset = Vector3.new(xoffset, yoffset, zoffset)
         local percentage = (timeStamp / duration)
         intensity *= percentage
    until timeStamp >= duration

    humanoid.CameraOffset = Vector3.new(0, 0, 0)
end

you should probably calculate dt but I was to lazy

wouldn’t this go like this (btw I want it linear)

percentage = 0.1 (0.1 increment in this scenario)
intensity = 100

> intensity = 100 * 0.1 = 10
> intensity = 10 * 0.2 = 2
> intensity = 2 * 0.3 = 0.6
...

Would this even do anything? You’re tweening the numValue but the intensity is constant, the tween would be useless from what I’m seeing.

Just do

currentIntensity = startIntensity - (currentTime / duration) * startIntensity

you can calculate the currentTime like this (using heartbeat is usefull in this case:

local last = tick()

runService.HeartBeat:Connect(function(dt)
local currentTime = tick() - last
last = tick()
--Code
end)

Example:

duration = 8
intensity = 5
TIME | INTENSITY
1 | 5 - (1 / 8) * 5 = 4,375

do you mean something like this?

local last = tick()

runService.HeartBeat:Connect(function(dt)
    local currentTime = tick() - last
    local percentage = currentTime / duration
end)

or am I wrong?

just redid my code, is this better?

function camStuff.GradualScreenshake(
    humanoid: Humanoid,
    duration: number,
    intensity: number
)
    local startTime = time()
    intensity *= 100
    
    local connection = runService.Heartbeat:Connect(function()
        task.wait()
        local endTime = time()
        local currentIntensity = intensity - (intensity * (endTime / duration)) --if percentage is 1, then deletes everything
        currentIntensity = math.round(currentIntensity)
        currentIntensity = math.clamp(currentIntensity, 1, 1000000)

        local xoffset = math.random(-currentIntensity, currentIntensity) / 500
        local yoffset = math.random(-currentIntensity, currentIntensity) / 500
        local zoffset = math.random(-currentIntensity, currentIntensity) / 500 

        humanoid.CameraOffset = Vector3.new(xoffset, yoffset, zoffset) 
    
        if endTime - startTime >= duration then
            connection:Disconnect()
            --will this disconnect correctly and stop the function?
        end
    end

    humanoid.CameraOffset = Vector3.new(0, 0, 0)
end

idk if replacing repeat with RunService is viable tho

Cant you just use ezcamshake? Pretty sure it can do that