How to Make a Part Sway?

Last 2 Weeks Ago, I tried to do a script to make certain pats sway and move. I was trying to make swaying leaves for trees in my showcase. The parts had a decal on of a realistic leaf as a test, when the script is inputted, it does work well along with other parts such as unions. However, there is a problem, when the script is inputted, the rotated position of the part inputted with the script faces a specific direction. I don’t know which part of the script was wrong, I tried to mess around with it. I’m a loser when it comes to scripting since I only do building and just tried scripting. Can anyone help with this?

Here is the script:

pos = script.Parent.Position
pos = Vector3.new(pos.x, pos.y-0.2, pos.z)
x = 0
z = 0
T = -99999
tall = script.Parent.Size.Y / 2
math.randomseed(tick())
rand = (math.random(0,20))/10
while true do
x = pos.x + (math.sin(T + (pos.x/5)) * math.sin(T/9))/3
z = pos.z + (math.sin(T + (pos.z/6)) * math.sin(T/12))/4
script.Parent.CFrame =
	CFrame.new(x, pos.y, z) * CFrame.Angles((z-pos.z)/tall, 0,(x-pos.x)/-tall)
	wait()
	T = T + 0.12
end

In a post last week - Help With Animation Script
I also tried to reach out but the only advice is to use the module of windshake, however I just can’t wrap my head around it, I’m lacking knowledge which to put and what to put in the scripts so it didn’t progress.

I originally pasted a script i found but in the middle of typing the issues I realized it was the same script

it works but some issues with it is you can not have your tree rotated on an angle, it just puts it straight upright. The other issue is, it has to be in model/part you want to have the sway, making it so you have more then one.

if you can edit it, so you don’t need that then that would be better… I tried the plugin mentioned its not that hard but it diffidently takes some time to get used to it. if you can get it so the parts pivot point is at the base of the tree you can get it to work for tree sway too.

that said what I was using was a palm tree i converted into a mesh with 2 parts it wasn’t that easy to get it to move together while having a bit of a separate movement for the leaf part.

1 Like

Thank you so much for the reply, I did use the scripts on some free models to make a base out of it and tried to make edits to it. Also, the script is actually working on parts and models and it actually sways. I’m just really confused as to why does it have a fixed angle when inputted into my parts but when it comes to the models I got the script from, the sway does work but it does not have a fixed angle.

1 Like

I have an alternative solution which requires much simpler scripting.

Say you want Part B to sway. Connect part B to part A (which must be above B) with a rope.

Then use Vector 3 to move part A from side to side.

1 Like

That is a nice simple idea, however, I think it would not result in a sway but rather more of a swing.

1 Like

Why not just use a sine wave?

In a local script (if you don’t need the effects to be synced it is very easy to do.

local Cycle
RunService.RenderStepped:Connect(function (Delta time)
    Cycle += Delta time
end

Use the Cycle into sim.

For a swing you can add it to a bezier curve

I am really confused why are other models are swaying with the script without being angle-fixed. Some models that I tested on, works properly and can even rotate the parts and still sway without angle fix happening.

I did not use any other formats other than the free model scripts I tried to mess with, since I am building often, I didn’t have knowledge how to script properly (I’m sorry but I am trying). For now, I do not understand how the script you mentioned works. But I will try to learn more about it. Thank you for the idea, I’ll mess the script and try to give it the result needed.

UPDATE: I saw few tutorials and videos about sine waves in roblox studio, however, it wasn’t the exact way I was hoping for. I was looking for to how to enable rotation on the part given with the sway script. But either way, thank you for the idea.

No problem, I used that same script for a while, until I realized it probably wasn’t a good idea to have a bunch of the same script running. I wish I could help more but I’m very new to scripting myself, so I wouldn’t know how to edit it, so parts are not fixed on the axis, so you wouldn’t need more then one

if you’re updating leaves and such, I really recommend just to use windshake. if you want to know how it works, you can even read its source code but I’m guessing you just want something that works.

the point of something like windshake is to make it easier, so by trying to avoid it because it’s complicated you are making it even more difficult for yourself.

I also checked the post and it looks like it has a gui editor, which means no code.

That’s okay, thank you for you help and replies, much appreciated. I’ll try to use other plugins and watch some tutorials to get the sway working. Thanks again.

I tried using the windshake model not the plugin, and it currently didn’t work yesterday. I have watched some tutorials how to get it work and it didn’t work. But I’ll try again, since something must have been wrong with my application. I’ll post an update.

cool. if it doesn’t work I’ll give it a shot myself and see what the problem is.

1 Like

Hello, I tried it on again and watched a tutorial on YT on how to apply the WindShake Model Plugin. I did try it on my showcase and on to another game, however, it doesn’t seem to be working on the studio nor the game. Here is the YT video I followed: How to add Wind Effect to objects (roblox studio) - YouTube

I did the same things and the same models. Blank workspace, no other parts other than the baseplate, no scripts or anything. I don’t get it, I used the same script as showed, here is mine:

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local WindShake = require(ReplicatedStorage:WaitForChild("WindShake"))

local WindDirection = Vector3.new(0.5, 0, 0.5);
local WindSpeed = 20;
local WindPower = 0.5;

WindShake:SetDeFaultSettings({WindDirection, WindSpeed, WindPower})

WindShake:Init() --emit

I used this script put it in the server script service. The WindShake model plugin is in the Replicated storage, none of them are disabled. I also used the tag editor shown on the video, I literally double-checked that the parts I want to move are tagged in the WindShake tag. But when I run it on the studio, it doesn’t work, nor does the game.

How can I achieve the moving trees with WindShake?

tried it myself. it worked. see this line.

WindShake:SetDeFaultSettings({WindDirection, WindSpeed, WindPower})

you wrote SetDeFaultSettings, it should be SetDefaultSettings. a typo. also, try sending the error message next time. I could have figured this out without even testing it if I had the error :slightly_smiling_face:

image

1 Like

Thank you so much! I finally got it to work! Thank you for the correction, this helped a lot!

UPDATE: Although it works really well on the studio, I’m afraid that it doesn’t work really well on the game, the parts that were moving on the studio are barely moving at the game. Is there any other way to fix this? You can test the game here → MODEL TESTING - Roblox I don’t know if it’s my connection, device or the problem is really in the game. You can check it out if the problem is the same.

sorry for the late reply.

actually, on my computer I can see them moving. it obviously depends a lot on how good your computer or your internet is (my machine is pretty slow, but connection is fast). you might see performance differences between the studio and your game because as far as I’m aware, studio gives you a “local development server” which means it is both playing and running the roblox server from your own computer. connecting to your own computer is of course much faster than connecting to roblox’s so it might seem faster.

to fix this problem, don’t use a server script. use a local script if you’re not already. any visual effects should go in localscripts to prevent these connection issues.

if you want to run this on for example a phone, it might be better to leave this feature out to prevent lag but you should test and see how bad it gets first. if the wind just isn’t giving the effect you want in general, you can change the WindSpeed or WindPower. if you still have problems you might want to leave a reply on the module, he probably knows more than I do.

1 Like

Noted. Thank you so much for the help! I’ll test it on a mobile device as well, if it does have a problem in the script, I’ll have to look it up. Thanks!

1 Like

UPDATE: I gave a review on the game on mobile and it’s still choppy. I tried to take the script out in the server script and put it on workspace. However, the performance is still choppy. I also changed the Hz up and down and still got no results. Thus, making the conclusion of sending a reply on the module post.

Can i see the code that performs the swaying? I may be able to help.
(Also, there is a windshake plugin you could probably use to add wind instead, it is very cheap. You can find information about it on this post)