How to make an image vibrate

Hello everyone!

I’m trying to make a jumpscare with an image and I want the image to vibrate.

Here is my script:

while true do
	local x = math.random(-0.05, -0.15)
	script.Parent.Position = UDim2.new(x,0,0.5,0)
	wait(0.1)
end

This script is meant to get a new random number;
local x = math.random(-0.05, -0.15)

Then apply it to the image position;
script.Parent.Position = UDim2.new(x,0,0.5,0)

Does anyone know why it doesn’t work?

Thanks in advance!

1 Like
while true do
	script.Parent.Position = script.Parent.Position + UDim2.new(math.random(0.05, 0.15),0,0.5,0)
	wait(0.1)
end
1 Like

No, I’m trying to set it to a position between -0.05 and -0.15.

Thanks in advance!

1 Like

ok you know u can just set it by yourself instead of me setting it

1 Like

If you read the actual post, I mention that it doesn’t work :confused:

1 Like
local image = script.Parent

while task.wait(0.1) do --change time if necessary
	local randomOffset = math.random(-1, 1)/10 --change range if necessary (equal values around 0 make it move around the origin point)
	image.Position = UDim2.new(image.Position.X.Scale, image.Position.X.Offset-randomOffset, image.Position.Y.Scale, image.Position.Y.Offset)
end
1 Like
local oldpos = script.Parent.Position
while true do
	script.Parent.Position = script.Parent.Position + UDim2.fromOffset(math.random(1,10),math.random(1,10))
	wait()
	script.Parent.Position = oldpos
end

this script is way better and work flawless

Calm down a bit up there

This can easily be chalked up to math.random only returning integers and the minimum number being the first parameter/second being the maximum, in which more can be read here.
You could just do math.random(-15,-5)/100 and it’ll give you the result you’re looking for.
Additionally, I don’t recommend doing while true do

while wait(0.1) do
	local x = math.random(-15,-5)/100
	script.Parent.Position = UDim2.new(x,0,0.5,0)
end
1 Like
local image = script.Parent

while task.wait(0.1) do --change time if necessary
	local randomScale = math.random(-1, 1)/10 --change range if necessary (equal values around 0 make it move around the origin point)
	image.Position = UDim2.new(image.Position.X.Scale + randomScale, image.Position.X.Offset, image.Position.Y.Scale, image.Position.Y.Offset)
end

Just realised you were trying to modify scale not offset.

Cheers mate.

I ended up changing the wait time and also the math.random parameters for a smoother transition.

Thanks for the help everyone!

Change wait() for task.wait() too, when used in a loop with small float arguments it’s more accurate/reliable.

1 Like

Yea sorry, forget that exists;

Using RenderStepped as well might be even more optimized than using a wait loop.

That’s typically reserved for Camera/Character manipulation only, I’d suggest using HeartBleed or Stepped.

1 Like