How can I make my screen shake?

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
65 Likes

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
13 Likes

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.

1 Like

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

10 Likes

Documentation - Roblox Creator Hub 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.

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.

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.

2 Likes

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.

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

1 Like

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.

5 Likes

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

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

Thanks! That worked.

I will definitely try that out and see what happens.

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:

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

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.

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

1 Like

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

No problem. Whenever you need help, just ask! :smile: