Basically I want my screenshaker to become a function, on which it occurs when the function is mentioned, although since I have already tried this it hasn’t worked.
Along with the screenshaker, I want it to fade out after 5 seconds.
THE SCREENSHAKER:
local plr = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local Xintensity = 20
local Yintensity = 20
game:GetService("RunService").RenderStepped:Connect(function()
local x = math.random(-Xintensity, Xintensity)/100
local y = math.random(-Yintensity, Yintensity)/100
cam.CFrame = cam.CFrame * CFrame.new(x,y,0)
end)
Is it something like this? I know it looks a bit rough, perhaps you can refine it later, but for now it should hopefully do the trick. (Explanation in comments)
local plr = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local Xintensity = 20
local Yintensity = 20
local connection
local count = 0
local function screenShake()
connection = game:GetService("RunService").RenderStepped:Connect(function()
local x = math.random(-Xintensity, Xintensity)/100
local y = math.random(-Yintensity, Yintensity)/100
cam.CFrame = cam.CFrame * CFrame.new(x,y,0)
count += 1
if count >= 300 then -- when count has reached 300 (random number idk) it will slowly fade
Xintensity -= .2
Yintensity -= .2
end
if Xintensity < .5 then -- Stops, resets values and disconnects from renderstepped before dividing by 0.
connection:Disconnect()
count = 0
Xintensity = 20
Yintensity = 20
end
end)
end
Edit* You can of course adjust values to fit your needs, the .2 for fade and 300 count were just randomly selected.
Here, I remade the script for you with customizable fade time and fade transition
local plr = game.Players.LocalPlayer
local cam = game.Workspace.CurrentCamera
local runService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local ShakeintensityX = script.ShakeIntensityX
local ShakeintensityY = script.ShakeIntensityY
ShakeintensityX.Value = 20 --Set the number to the shake intensity you want
ShakeintensityY.Value = 20 --Set the number to the shake intensity you want
local function ScreenShaker(fadeTime)
print("Shake started")
local tweenInfo = TweenInfo.new(
fadeTime,
Enum.EasingStyle.Linear,
Enum.EasingDirection.Out,
1,
false,
0
)
local tweenX = TweenService:Create(ShakeintensityX, tweenInfo, {Value = 0}):Play()
local tweenY = TweenService:Create(ShakeintensityY, tweenInfo, {Value = 0}):Play()
local x
local y
local ShakeEffect = runService.RenderStepped:Connect(function()
local Xintensity = ShakeintensityX.Value
local Yintensity = ShakeintensityY.Value
if Xintensity > 0 then
x = math.random(-Xintensity, Xintensity)/100
else
x = 0
end
if Yintensity > 0 then
y = math.random(-Yintensity, Yintensity)/100
else
y = 0
end
cam.CFrame = cam.CFrame * CFrame.new(x,y,0)
end)
wait(fadeTime)
ShakeEffect:Disconnect() --Stops the shake effect.
print("Shake ended")
end
wait(5) --remove this is it's not needed
ScreenShaker(5) --When you call the function, set this number to how long you want the shake to last
Make sure to create two number values under the script, one named “ShakeIntensityX” and the other named “ShakeIntensityY” otherwise it won’t work.
Quick question, is there anyway to make the function fire by a remote in replicated storage? a script in the workspace should have some code in it which should fire a remote and trigger the localscript.