Brick Tweening changing position

So currently I am playing around with some code, currently this code just tweens a brick to move in a linear straight direction. Code below.

local TweenService = game:GetService("TweenService")
 
local part = game.Workspace.Location1
part.Position = Vector3.new(558, 0.5, -75.7)
part.Anchored = true
part.Parent = game.Workspace
part.BrickColor = BrickColor.Black()
 
local tweenInfo = TweenInfo.new(
	4, -- Time
	Enum.EasingStyle.Linear, -- EasingStyle
	Enum.EasingDirection.In, -- EasingDirection
	
	-1, -- RepeatCount (when less than zero the tween will loop indefinitely)
	true, -- Reverses (tween will reverse once reaching it's goal)
	0 -- DelayTime
)
 
for count = 1, 30 do
	local tween = TweenService:Create(part, tweenInfo, {Position = Vector3.new(575.5, count, 50.3)})
end

 
tween:Play()
wait(10)
 -- cancel the animation after 10 seconds

What I am trying to accomplish is getting the brick to move in the type of direction below.

How would I be able to constantly change tween position, I tried to do a loop as you can see but it wont work cause it becomes nil.

1 Like

Look up the mathematical equation for a sine wave and you get the wiki answer:

2 Likes

I would definitely recommend a Render Step solution here. This can be accomplished fairly easily with a little sine math. Here’s what that would look like:

--Services
local RunService = game:GetService("RunService")

--Variables
local step = 0.1

--Components
local connection
local increment = 0

--Loop
connection = RunService.RenderStepped:Connect(function()
 increment = increment + step
 workspace.Part.Position = Vector3.new(0,math.sin(increment),increment)
end)

--Disconnect
wait(10)
connection:Disconnect()

Result:
https://gyazo.com/36777ce8283f45841a560345a98c05ec

Hope this helps!

4 Likes

Thank you so much for the help, is it possible for you to explain in a simple way of what RunService does and how it influences it here? I am new to scripting as well as tweening so understanding better would be great :slight_smile:

Sure! There’s a few things you can do with RunService, the most common use case being this RenderStepped connection. It essentially fires once per frame rendered. So if my game is at 60fps, this function fires 60 times per second. Here’s some more details on it if you want to learn more:

RunService API

2 Likes