You can write your topic however you want, but you need to answer these questions:
What do you want to achieve? Keep it simple and clear!
I want to tween a train (Union currently) towards the next part everytime it reaches the correct part.
What is the issue? Include screenshots / videos if possible!
I don’t know how i would even start.
What solutions have you tried so far? Did you look for solutions on the Developer Hub?
Dev forum and Dev hub doesn’t have specific answers.
That shouldn’t be too hard to do, you can just set it up like so:
local Train = --where your train would be
local TweenService = game:GetService("TweenService")
local CanTouch = true
local TrainTweenInfo = TweenInfo.new(--how long you want the tween to last in seconds , Enum.EasingStyle, Enum.EasingDirection)
local TrainTweenProperties -- this should remain blank, so for convenience, you can change it
--whenever you need.
local TrainTween -- Also will be edited on-site.
local TrainTweenFinishedConnection -- The train will be allowed to move again when it's finished tweening.
function OnFinish()
CanTouch = true
end
function OnTouch(GoalPart)
if CanTouch ~= false then
if string.match(GoalPart.Name,"GoalPart") then
CanTouch = true
TrainTweenProperties = {Position = GoalPart.Position}
TrainTween = TweenService:Create(Train,TrainTweenInfo,TrainTweenProperties)
TrainTween:Play()
TrainTweenFinishedConnection = TrainTween.Finished:Connect(OnFinish)
end
end
end
TrainTween.Touched:Connect(OnTouch)
Basically, what we’re doing here is making it so whenever the train touches what we’ll call a GoalPart (where the train will go, they act like waypoints) it checks if GoalPart is in it’s name so you can name your GoalParts freely like GoalPart1, GoalPart2 and so on.
Next we create the tween with it’s properties now that we have access to the GoalPart’s position, and play it. Now, here’s where CanTouch comes into place. When the part is first touched, CanTouch is set to false so the tween cannot fire again before the currently playing one is finished, where TrainTween.Finished comes into place. When that event fires, CanTouch is set to true again.
Now, here, i’m only giving you what you asked for, but be warned that it will look extremely ugly. What you should do is make actual tracks, and use things like BodyForce or BodyVelocity to push the train along, making it look natrual. But this is how you do it with tweens. Let me know if there are any problems with the script or anything like that.
two problems. 1.
How would i go about making the speed controllable for the player that is driving? Heres a quick (terrible looking, I know) GUI i wipped up. I added some variables to the GUI like speed and allowedspeed in the form of numbervalues.
(The GUI is already functional and adaptive to the numbers you give it using the numbervalues. Same goes for every text that is written %like this%)
Heres what my idea would be to do this:
have the tween time adapt by speed using some kind of equasion? Im not totally sure.
2.
the train is a model. Do i need to root it? If yes, how do I root something? Sorry if I’m asking for a lot but this is my first time doing some advanced tweeting. OR i could make the train a nice mesh somehow. Idk whats best.
oh god more problems
found the solution for this one. A typo in your code. local traintweeninfo = tweeninfo.new( instead of local traintweeninfo = tweeninfo.new()
help
I think i fixed your code, it no longer gives errors. However, nothing is happening! This is the code:
local Train = workspace.trainunion
local TweenService = game:GetService("TweenService")
local CanTouch = true
local TrainTweenInfo = TweenInfo.new(5)--how long you want the tween to last in seconds , Enum.EasingStyle, Enum.EasingDirection)
local TrainTweenProperties -- this should remain blank, so for convenience, you can change it whenever you need.
local TrainTween -- Also will be edited on-site.
local TrainTweenFinishedConnection -- The train will be allowed to move again when it's finished tweening.
function OnFinish()
CanTouch = true
end
function OnTouch(GoalPart)
if CanTouch ~= false then
if string.match(GoalPart.Name,"GoalPart") then
CanTouch = true
TrainTweenProperties = {Position = GoalPart.Position}
TrainTween = TweenService:Create(Train,TrainTweenInfo,TrainTweenProperties)
TrainTween:Play()
TrainTweenFinishedConnection = TrainTween.Finished:Connect(OnFinish)
end
end
end
Train.Touched:Connect(OnTouch)
hopefully, you react soon. I’ll be working on the GUI in the meantime.
First problem i know it was 2 years ago that this was created but using tween service may not be the best way as the only way to control the speed would be by changing the time it takes you to reach each point but for different points it would be slower/faster depending on the distance between them,
Example for a train that takes 20 seconds to cover 20 studs that a stud per second, however if I then change it to 40 studs in 20 seconds thats 2 studs a second, double the speed.