Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How to have a piece of machinery vibrate/move slightly?

Hello fellow programmers,

I have a question I could not find the answer for, so I am hoping to find an answer here.

My question is: How would I have a piece of machinery (in this case, a pulley), vibrate, to create an aesthetic as if it were moving/working? I have written some code below, but the object does not seem to move. However, if I remove the second line of code (which is labeled --(KL)), the object would move, but only up, as expected. My intention was for the object to move up, and after the wait duration, it would move back down and repeat. How this script works is that when a player would click a button, the script would be enabled for two seconds, and then disabled. During this two second duration, the machine will seem to be 'working', releasing smoke and moving the pulley slightly. It would be nice to still use a similar CFrame to fix this, but I am willing to learn a new solution. There is no error output, and technically this should work, but I believe what happens is that the part just doesn't move fast enough for the position difference to be shown on display, but I'm not sure. Either ways, here is the code:

local part = script.Parent

while true do
	part.CFrame = CFrame.new(part.CFrame.X, part.CFrame.Y + .5, part.CFrame.Z)
	wait(.1)
	part.CFrame = CFrame.new(part.CFrame.X, part.CFrame.Y - .5, part.CFrame.Z) --(KL)
end

Thank you for reviewing my question,

--RuskiKrolik

2 comments

0

Just want to say that you don't need to be so formal, but you should accept Diezel's answer, it is correct.

0

My main goal with being formal is to be easy to understand, which in result would have my question answered sooner. I also wanted to inform the respondent what is happening in the situation, so there would not have to be a back and forth commenting about the respondent needing more information.

1 answer

✓ Accepted answer

The problem is that after the 2nd Cframe change in the loop, it goes straight back to the first CFrame change in a split second. You simply need to add another wait(.1) after line 6. or at the beginning of the loop.

local part = script.Parent

while true do
	wait()
    part.CFrame = CFrame.new(part.CFrame.X, part.CFrame.Y + .5, part.CFrame.Z)
    wait()
    part.CFrame = CFrame.new(part.CFrame.X, part.CFrame.Y - .5, part.CFrame.Z) --(KL)
end

7 comments

0

Thank you for the information!

0

The problem with this, though, is that it is not as fast as I need it to be. Would using Tween Services be a better option?

0

I would try saying wait() first (that's the smallest possible wait time)

0

I have already tried wait() and wait() with different timers, even a five second delay. Every time the script is enabled, it only moves up by the given .5 amount, but does not move back down. I have put print statements after the CFrame change scripts, and realized that even though the part does go down (because the print statement says so, since it is after the down movement line), it wontgodown

0

I will try and use tweening and see if that would work.

0

Try it with the edits I just made.

0

Oh, thanks for the help! I didn't thoroughly read your code and did not see the extra wait(). Thank you for the help!

This discussion is preserved as it was. New answers, comments, and votes are closed.