function ShakeCamera.playShake(self: self): ()
local shakeOffset = Vector3.new(
MATH_RANDOM(-self.ShakeIntensity, self.ShakeIntensity),
MATH_RANDOM(-self.ShakeIntensity, self.ShakeIntensity),
MATH_RANDOM(-self.ShakeIntensity, self.ShakeIntensity)
)
local originalOffset = VECTOR_3(0, 0, 0)
local currentTime = os.clock()
local deltaTime = (currentTime / self.ShakeSpeed)
humanoid.CameraOffset = originalOffset + shakeOffset * (1 - deltaTime)
humanoid.CameraOffset = originalOffset
end
Are you firing the function? And how does it not work, put some print statements to see if it runs or not.
local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid:: Humanoid
local camera = workspace.Camera
--libs
local TABLE_CLEAR = table.clear
local VECTOR_3 = Vector3.new
local MATH_RANDOM = math.random
local ShakeCamera = {}
ShakeCamera.__index = ShakeCamera
function ShakeCamera.new(shakeIntensity: number, shakeSpeed: number): self
assert(type(shakeIntensity) == "number" and assert(type(shakeSpeed) == "number"))
local self = setmetatable({}, ShakeCamera)
self.ShakeIntensity = shakeIntensity
self.ShakeSpeed = shakeSpeed
return self
end
function ShakeCamera.playShake(self: self): ()
local shakeOffset = Vector3.new(
MATH_RANDOM(-self.ShakeIntensity, self.ShakeIntensity),
MATH_RANDOM(-self.ShakeIntensity, self.ShakeIntensity),
MATH_RANDOM(-self.ShakeIntensity, self.ShakeIntensity)
)
local originalOffset = VECTOR_3(0, 0, 0)
humanoid.CameraOffset = originalOffset + shakeOffset
task.wait() -- if i add a time this time is not valid...
humanoid.CameraOffset = originalOffset
end
function ShakeCamera.setShakeIntensity(self: self, shakeIntensity: number): ()
assert(type(shakeIntensity) == "number")
self.ShakeIntensity = shakeIntensity
end
function ShakeCamera.setShakeSpeed(self: self, shakeSpeed: number): ()
assert(type(shakeSpeed) == "number")
self.ShakeSpeed = shakeSpeed
end
function ShakeCamera.remove(self: self)
TABLE_CLEAR(self)
setmetatable(self, nil)
end
export type self = typeof(ShakeCamera.new(...))
return {
new = ShakeCamera.new
}
Can I see the script you’re firing from? You can also try to make a while loop and just fire that function.
I understood the error. Since I’m using a RunService for a magnitude system, I’m calling the shake function at the same time. How can I remedy this? I tried using a coroutine.
By the way, there’s no need to create an object for a camera shake.