How do i get the opposite of a number

  1. What do you want to achieve? I want the opposite of a number example: 1 would be 0 and 0 would be 1.

  2. What solutions have you tried so far? I have tried using math.abs but that just gave me the same number and other posts that looked similar didnt have what i needed.

my script: (local script)

local char = game.Players.LocalPlayer.Character
while wait() do
	local Distance = (char.HumanoidRootPart.Position - game.Workspace.ScreenShaker.Position).Magnitude
	local Rounded = math.round(Distance)
	local intensity = math.abs(1 - Rounded) --this is the line where i get problems

it prints a low number when i am close to the part and a high number when im far away from it.
this wont work as the screenshake should be intense when im close to the part and not when im far away from it.

This is for a screenshake effect.

You must have an effective rolloff distance for the effect though. So just use that as the basis for maxdist and subtract the Magnitude distance, ie:

local rolloff = 100
local Distance =  math.round((char.HumanoidRootPart.Position - game.Workspace.ScreenShaker.Position).Magnitude)
local intensity = rolloff - Distance

In the above, if the user is 100 studs away, then intensity = 0. If they are 10 studs away, then intensity of 90. I hope that makes sense.

You can add a radius for linear interpolation for the screenshake effect, assuming 0 is highest and 1 is the lowest intensity for your effect.

local ScreenShakeRadius = 50
local dist = (HumRp.Position - workspace.ScreemShaker.Position).Magnitude
if dist > ScreenShakeRadius then
   --cancel the effect and don't continue with the bottom

local intensity = dist / ScreenShakeRadius
intensity = math.abs(intensity - 1) --inverse
if intensity < 1 then
   --do the screen effect when it's between 0 and 1

That worked! i just had to clamp it to 0 and the rolloff so that it doesnt go into the negatives

Edit: now the while loop doesnt stop:

local char = game.Players.LocalPlayer.Character

 if child.Name == "A-10" or child.Name == "X-10" then
	while child do
	local Distance = (char.HumanoidRootPart.Position - child.Position).Magnitude
	local Rounded = Distance
	local rolloff = 120
	local intensity = math.random(0.1, math.clamp(rolloff, 0, 1))
	local final = rolloff - Rounded
	local clamp = math.clamp(final, 0, intensity)
	char.Humanoid.CameraOffset =, clamp), math.random(0, clamp), math.random(0, clamp))		

the “A-10” or “X-10” are the monters and they get cloned from ReplicatedStorage in a script. at the end they get destroyed but the while loop doesnt stop.