Need help with a TweenService script

ok so I’m trying to move a hot air balloon around my map, I set up way points for it to travel to.

(Waypoints)
waypoints

ways

(Script)

local balloon = script.Parent

local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(
	
	15, -- Time
	Enum.EasingStyle.Linear, -- Easinmg Style
	Enum.EasingDirection.Out, -- Easing Direction
	-1, -- Repeat Count
	false, -- Reverse
	0 -- Delay Time
)

local tween = TweenService:Create(balloon, tweenInfo, {Position = game.Workspace.Waypoints["waypoint 2"].Position})

tween:Play()

tween.Completed:Wait()

but I can’t figure out how to add in the other waypoints for it to continue to, I’ve tried different ways but have had no luck making it work.

when the tween completes, run another tween, this time moving to 2, then to 3, and so on

I’ve tried doing it like this but no luck it just restarts from 1 to 2 again and just keeps repeating.

local tween = TweenService:Create(balloon, tweenInfo, {Position = game.Workspace.Waypoints["waypoint 2"].Position})

tween:Play()

tween.Completed:Wait()


local tween = TweenService:Create(balloon, tweenInfo, {Position = game.Workspace.Waypoints["waypoint 3"].Position})

tween:Play()

tween.Completed:Wait()


local tween = TweenService:Create(balloon, tweenInfo, {Position = game.Workspace.Waypoints["waypoint 4"].Position})

tween:Play()

tween.Completed:Wait()


local tween = TweenService:Create(balloon, tweenInfo, {Position = game.Workspace.Waypoints["waypoint 1"].Position})

tween:Play()

tween.Completed:Wait()

idk if this would work,
script:

local balloon = script.Parent



local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(

	15, -- Time
	Enum.EasingStyle.Linear, -- Easinmg Style
	Enum.EasingDirection.Out, -- Easing Direction
	-1, -- Repeat Count
	false, -- Reverse
	0 -- Delay Time
)

local goal1 = {
	Position = game.Workspace.Waypoints["waypoint 2"].Position
	
}

local goal2 = {
	Position = game.Workspace.Waypoints["waypoint 3"].Position
}

local goal3 = {
	Position = game.Workspace.Waypoints["waypoint 4"].Position
}

local tween1 = TweenService:Create(balloon, tweenInfo, goal1)

local tween2 = TweenService:Create(balloon, tweenInfo, goal2)

local tween3 = TweenService:Create(balloon, tweenInfo, goal3)

tween1:Play()

tween1.Completed:Wait()
tween2:Play()
tween2.Completed:Wait()
tween3:Play()
tween3.Completed:Wait()

but try this script!

First we have to loop the code so let’s do that:

while true do
   ...
end

Now let’s make an array with all the CFrames (CFrames are positions with the orientation in radiants)

local CFrames = {...}

An example could be:

local CFrames = {
   workspace:WaitForChild("Spot1").CFrame, -- dont forget the comma 
   workspace:WaitForChild("Spot2").CFrame 
}

Then we will make an index value for the while loop, we wont use a for loop, because we cannot make that run for ever.

local i = 1

Then each time a tween is finished we will have to add a value to it

i += 1

Then if the index is bigger than the size of the array we will have to set it back to 1. We can use % with that.

local arraySize = #CFrames
i = (i-1)%arraySize + 1

Quick showcase of %

1%5 = 1
5%5 = 0
6%5 = 1
8%5 = 3
12%5 = 2
-- etc.

Now we first remove a value from the index because we still want it to be 4 and we dont want it to be 0. After we cant forget to add the value back.

Now if we combine this with i += 1 it would become

i = (i-1)%#CFrames+ 2

Now it would look something like this:

local CFrames = {...}

local i = 0
while true do
   i = (i-1)%#CFrames+ 2
   local nextCFrame = CFrames[i] -- CFrame you want it to set
   local tween = TweenService:Create(balloon, tweenInfo, {
      ["CFrame"] = nextCFrame 
   })
   tween:Play()
   tween.Completed:Wait() -- wait for it to complete
end

so your saying like this?

local balloon = script.Parent

local TweenService = game:GetService("TweenService")

local tweenInfo = TweenInfo.new(
	
	15, -- Time
	Enum.EasingStyle.Linear, -- Easinmg Style
	Enum.EasingDirection.Out, -- Easing Direction
	-1, -- Repeat Count
	false, -- Reverse
	0 -- Delay Time
)

local CFrames = -{game.Workspace.Waypoints["waypoint 1"],
	game.Workspace.Waypoints["waypoint 2"],
	game.Workspace.Waypoints["waypoint 3"],
	game.Workspace.Waypoints["waypoint 4"]
}

local i = 0
while true do
	i = (i-1)%#CFrames+ 2
	local nextCFrame = CFrames[i] -- CFrame you want it to set
	local tween = TweenService:Create(balloon, tweenInfo, {
		["CFrame"] = nextCFrame 
	})
	tween:Play()
	tween.Completed:Wait() -- wait for it to complete
end

You Added - before the {} and you have to get the CFrame property of each waypoint so:

local CFrames = {
   workspace:WaitForChild("Waypoints"):WaitForChild("waypoint 1").CFrame,
   workspace["Waypoints"]:WaitForChild("waypoint 2").CFrame,
   workspace["Waypoints"]:WaitForChild("waypoint 3").CFrame,
   workspace["Waypoints"]:WaitForChild("waypoint 4").CFrame,
}

The rest is correct, but make sure to use WaitForChild more often, so the script doesnt load before these positions do.

1 Like

ok i’ll keep this in mind, but unfortunately now it’s not moving at all. lol

did change anything still just goes from 1 to 2

1 Like

Move it off from any of the waypoints, it could be trying to move to waypoint 1, while it is already on it. Then it would take 15 seconds until it does start moving.

for me it did, i tryed it but i made a mistake cuz it dint completed it all it just goes at the same time.

What I meant is, can you change the position of the balloon inside ROBLOX studio. Move it away from any Waypoints, put it on Waypoint 4 or change the time it takes to move from 15 to 1 while testing.

Oh I have noticed another issue… Set the Repeat Count to 0. Not to -1, because then it would only play the first tween on repeat.

So it would be:

local tweenInfo = TweenInfo.new(
	15, -- Time
	Enum.EasingStyle.Linear, -- Easing Style
	Enum.EasingDirection.Out, -- Easing Direction
)

Because all the other Values are already set to default we can just remove those.

(Make sure to either wait 15 seconds or to move the balloon off from Waypoint 1)

nevermind i figured it out i just added another.

workspace["Waypoints"]:WaitForChild("waypoint 1").CFrame,

and it works perfect thank you so much!!!

That has to do with this part.
To fix it we can jut do

i = (i) % (#CFrames) + 1

I accidentally made a mistake on my part.
Now let’s simulate it

i = 1 -> i = 2
i = 2 -> i = 3
i = 3 -> i = 4
i = 4 -> i = 1

It works correct, you can test it via google just insert the equation in there and replace i with 1 to 4.

1 Like

ok sweet thanks again you’ve been a big.

Are you for hire by any chance?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.