Problem with tween speed

How can I make all the tweens go at the same speed, and finish within the time?
I set up multiple parts which are path for the tweens but Monster goes slower and faster depending on distance.
Code:

local runDuration = 65
    for i,v in pairs(script.Parent.points:GetChildren()) do
        local properties = {
            CFrame = v.CFrame
        }
        local time = runDuration/#script.Parent.points:GetChildren() --here
        print(time)
        local t = game.TweenService:Create(script.Parent.Monster.HumanoidRootPart,TweenInfo.new(time,Enum.EasingStyle.Linear),properties)
        t:Play()
        t.Completed:Wait()
    end
1 Like

They’re not gonna start at the same time when you wait for each one to finish are they? nvm I’m so stupid I thought it was finish at the same time not within

Using Task


By placing something in task.spawn(function() it will do it whilst continuing with the code beneath it.

Your code with this implemented:

local runDuration = 65
for i,v in pairs(script.Parent.points:GetChildren()) do
    task.spawn(function() -- With this the loop will go through everything making all the task start at the same time
        local properties = {
            CFrame = v.CFrame
        }
        local time = runDuration/#script.Parent.points:GetChildren() --here
        print(time)
        local t =     game.TweenService:Create(script.Parent.Monster.HumanoidRootPart,TweenInfo.new(tim    e,Enum.EasingStyle.Linear),properties)
        t:Play()
        t.Completed:Wait()
    end)
end

No, it’s not what I’m looking for.
Lemme explain again:
This is a sequence of positions which are path for monster to follow.
The problem is that each point has other speed than others, it depends on distance.
I want them to have the same speed.
Example of what happens now:
x --------- x (extremely fast,10s)
x - x (really slow, 10s)
What im trying to get:
x ----------- x
is the same as
x - x

Tweens’ speeds are determined by how far their goals are to their current values, and how much time should it take.

If a Tween plays for 1 second, and it has to move a Part 5 studs from its position, it looks normal. Make another tween that moves another Part but from 15 studs away, and it moves faster in the same amount of time.

You must calculate the actual time for properties to reach their destination based on how far they are. A simple solution should work like this (using Part.Position):

local Studs_Per_Second = 5

---

-- Script inside a model with multiple parts, one named "T", used for goal position.

local Studs_Per_Second = 15

local Tween_Service = game:GetService("TweenService")
local Goal_Position = script.Parent.T.Position

task.wait(5) -- video demonstration purposes, not important in actual use.

for _, v in ipairs(script.Parent:GetChildren()) do
	if not (v.Name == "T") then
		local Tween_Info = TweenInfo.new(
			(v.Position - Goal_Position).Magnitude / Studs_Per_Second,
			Enum.EasingStyle.Linear
		)
		
		local New_Tween = Tween_Service:Create(
			v, 
			Tween_Info,
			{Position = Goal_Position}
		)

		New_Tween:Play()
	end
end

Demonstration (video seems to stutter. Roblox recording is just bad):

No effect at all :confused:
It still slows down and then goes in insane speed

Ok I figured it out.
So first you need to manually set time for 1st point (because there’s no previous one),
then you get Position of previous point and calculate distance between them 2.
Next you divide the distance and speed you want to.

Code:

	local runSpeed = 15
local points = script.Parent.points:GetChildren()
	for i,v in pairs(points) do
		local properties = {
			CFrame = v.CFrame
		}
		
		local point
		
		local time = 7 --you need to manually set first time
		if not(i==1) then --this will only be accurate if you're getting previous point
			point = points[i-1]
			local distance = (point.Position - v.Position).magnitude
			time = distance / runSpeed
		else
			point = points[#points]
		end
		
		local t = game.TweenService:Create(script.Parent.Monster.HumanoidRootPart,TweenInfo.new(time,Enum.EasingStyle.Linear),properties)
		t:Play()
		t.Completed:Wait()
	end
2 Likes