Hello!
I am trying to make a simple camera shake system using CameraOffset in the humanoid, however the code is not working. It only changes the CameraOffset the first time and after that it only prints.
game.Players.PlayerAdded:Connect(function(Player)
Player.CharacterAdded:Connect(function()
while true do
local X = math.random(-4, 4)
local Y = math.random(-4, 4)
local Z = math.random(-4, 4)
Player.Character:FindFirstChild("Humanoid").CameraOffset = Vector3.new(X, Y, Z)
print(Player.Character:FindFirstChild("Humanoid").CameraOffset)
wait(0.2)
end
end)
end)
I know this topic was 4 months ago, but it’s also the top result of Google so I figured I’ll post this wisdom here for any futuregoers working on their screen shake system and haven’t seen the CameraOffset value changing at all:
math.random() does not work with decimals
Check through where you’re using math.random() and make sure decimals aren’t going into them.
There’s your reminder, pros. Feel free to facepalm and go “Oohhhhh…” once you find your mistakes.
Try multiplying the decimal by 1000 and then dividing it by 1000 once you get to adding it to the CameraOffset property.
Examples
(These Examples should be in LocalScripts located under StarterPlayerScripts or a Tool)
Example
local plr = game.Players.LocalPlayer -- < BECAUSE THIS ONLY WORKS ON THE CLIENT
local variance = 0.05 -- < NOTICE HOW IT IS A DECIMAL. THIS MEANS IT WILL NOT
-- WORK WITH MATH.RANDOM
local newVariance = variance * 1000 -- < MULTIPLY IT BY 1000 TO REMOVE DECIMALS.
-- THIS ALSO HAS THE EFFECT OF ADDING MORE VARIANCE, WHICH IS NICE :sunny:
local YCamOffset = math.random(newVariance * -1, newVariance) -- < CREATE OFFSET.
-- IF YOU'RE GOING TO USE X AND Z CAM OFFSET TOO, JUST COPY AND PASTE
-- THE ABOVE LINE, CHANGE THE FIRST LETTER AND PUT THEM INTO THEIR
-- VECTOR3.NEW() SLOTS (x,y,z)
local newOffset = Vector3.new(0, YCamOffset, 0) / 1000 -- < DIVIDE IT BY 1000 NOW
-- THAT MATH.RANDOM() IS OUT OF THE WAY.
plr.Character.Humanoid.CameraOffset = newOffset -- < APPLY OFFSET
Example Without Comments For The Shameless Copy + Pasters
local plr = game.Players.LocalPlayer
local variance = 0.05
local newVariance = variance * 1000
local YCamOffset = math.random(newVariance * -1, newVariance)
local newOffset = Vector3.new(0, YCamOffset, 0) / 1000
plr.Character.Humanoid.CameraOffset = newOffset -- < APPLY OFFSET
do not reply unless i got these wrong, i haven’t tried these scripts in studio