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)
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
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
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.
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.
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.
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.
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.