How can I make my screen shake?

#1

Hello. My goal is to make my screen shake whenever I click but nothing seems to be working properly.
It only changes my CameraOffset once, but after that, it never moves. I’ve tried to ask other people but none have helped. I also tried to look at the Wiki for some information about the CameraOffset but nothing is helping. I’m pretty new to scripting tho.

wait()
local Player = game.Players.LocalPlayer
local Char = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Char.Humanoid
local Enabled = true
local Mouse = Player:GetMouse()
local x = math.random(-100,100)/100
local y = math.random(-100,100)/100
local z = math.random(-100,100)/100
Mouse.Button1Down:connect(function()
if not Enabled then return end
Enabled = false
for i = 1, 20 do
Humanoid.CameraOffset = Vector3.new(x,y,z)
print(i)
wait()
end
Humanoid.CameraOffset = Vector3.new(0,0,0)
Enabled = true
end)

2 Likes

#2

I think the problem is that x, y, and z never change. You give them a random value at the start of the script, but never change it after that. What you should be doing is generating a new x, y, and z each time you loop.

for i = 1, 20 do
    local x = math.random(-100,100)/100
    local y = math.random(-100,100)/100
    local z = math.random(-100,100)/100
    Humanoid.CameraOffset = Vector3.new(x,y,z)
    print(i)
    wait()
end
5 Likes

#3

Instead of using Humanoid.CameraOffset, try multiplying the Camera’s CFrame by some CFrame.Angles Value to achieve your desired effect.

Heres a sloppy example:

Mouse.Button1Down:connect(function()
for i = 1,5 do
workspace.Camera.CFrame = workspace.Camera.CFrame * CFrame.Angles(0.03,0,0)
wait()
for i = 1,5 do
workspace.Camera.CFrame = workspace.Camera.CFrame * CFrame.Angles(-0.03,0,0)
wait()
end
end)

This script would shake your camera a tad bit when clicked. This sloppy example would look pretty clunky, but you could add your own effects to make it look better, i, e Different angles, exponentially getting smaller.

However, if you only want to use Humanoid.CameraOffset, then all you need to do to make your camera return to its original state is to add another line of code like this:

local x = math.random(-100,100)/100
local y = math.random(-100,100)/100
local z = math.random(-100,100)/100
for i = 1, 20 do
Humanoid.CameraOffset = Vector3.new(x,y,z)
print(i)
wait()
end
for i = 1, 20 do -- second loop to return camera to original position
Humanoid.CameraOffset = Vector3.new(-x,-y,-z) -- negating offset
print(i)
wait()
end
2 Likes

#5

I honestly think it would be a bit choppy if you just run this. I would consider something that would work smoother.

I can’t think of any alternatives at the moment, but when I do, I’ll be sure to reply.

0 Likes

#6

Check out @Crazyman32’s EZ Camera Shake module. It is very easy to use and performs great looking and efficient shakes.

3 Likes

#7

https://wiki.roblox.com/index.php?title=Random#Random.new Use random.new() instead of math.random, which requires a seed to be inputted instead of random.new(), where if no parameters are given, will automatically put in a new randomized seed.

0 Likes

#8

I’m pretty sure math.random() was updated to use the same algorithm as Random.new(). The difference is that Random sports slightly more capabilities while math.random() is anchored to either a 0-1 value or a minimum and maximum. math.randomseed() is no longer a requirement.

0 Likes

#9

This is correct I think; I’ve been told math.random uses the Random class under the hood. Either way bringing up math.random versus Random.new() seems like a non-point in the scheme of this thread.

1 Like

#10

You can consider this randomizing method if you want to achieve what you want. This is specifically indicated for table values, but it can be used regardless.

0 Likes

#11

Ah, my mistake. I’ll just go ahead and delete my useless post now…

0 Likes

#12

It’s not that you have to delete your post if you’re wrong - the DevForum is a learning resource and a discussion platform. There are things you and I both don’t know. You didn’t know before, you know now. The point was relatively relevant in terms of what can be used to achieve a random effect (which is part of what OP requires, a way to fetch random numbers, so that it can be applied to a loop of some sort to make their screen shake). Just remember to stay on-topic and work towards helping OP find a solution.

1 Like

#13

As a general note, you probably should’nt delete your posts that were involved in a conversation since they add to the conversation of the post.

1 Like

#14

Oh okay sorry I’m really new to this. How would I go about undeleting a post? Edit : Found the button

0 Likes

#15

Thanks! That worked.

0 Likes

#16

I will definitely try that out and see what happens.

0 Likes

#17

It looks really great but how do you go about using it? I’m still pretty new to all this stuff so I wouldn’t know where to put all the scripts just yet. :sweat_smile:

0 Likes

#18

Examples are given in the post, as well as in the comments.

0 Likes

#19

If you are talking about where the ModuleScript is supposed to be, you just need to put it where a localscript can require it. For instance, ReplicatedStorage would be a suitable place.

0 Likes

#20

Make sure to put @ExtremeBuilder15 's answer as the solution if it worked!

1 Like

#21

Oh thank you ill try out that soon. Thanks for yhe heads up. :slight_smile:

0 Likes