Help with tweening bridge segment

i’m making a game that has a bridge that “builds itself” in front of you. it works fine, but i’d like to tween it upwards when it’s time to make a new segment. i’ve been trying to get it to work but i’m really inexperienced with TweenService so i’ve had no success.

if game:IsLoaded() == false then game.Loaded:Wait() end

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")

local player = Players.LocalPlayer

local startingPoint = workspace:WaitForChild("Start")
local bridgeSegment = script:WaitForChild("BridgeSegment")

local bridges = 0

RunService.Heartbeat:Connect(function(deltaTime)
	if player.Character == nil then return end
	if (startingPoint.Position - player.Character:GetPivot().Position).Magnitude > 8 then return end

	if bridges < 5 then
		local bridgeSegmentClone = bridgeSegment:Clone()
		bridgeSegmentClone:PivotTo(startingPoint.CFrame)
		--bridgeSegmentClone.Start:Destroy()
		--bridgeSegmentClone.End.Transparency = 1

		startingPoint:Destroy()
		startingPoint = bridgeSegmentClone.End
		
		bridgeSegmentClone.Parent = workspace
		
		bridges += 1
	end
end)

Make sure to have the necessary modules (TweenService) required at the top of your script., You can also modify the tweenInfo to adjust the duration and easing of the tween.

if game:IsLoaded() == false then
	game.Loaded:Wait()
end

local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local TweenService = game:GetService("TweenService")

local player = Players.LocalPlayer

local startingPoint = workspace:WaitForChild("Start")
local bridgeSegment = script:WaitForChild("BridgeSegment")

local bridges = 0

RunService.Heartbeat:Connect(function(deltaTime)
	if player.Character == nil then
		return
	end
	if (startingPoint.Position - player.Character:GetPivot().Position).Magnitude > 8 then
		return
	end

	if bridges < 5 then
		local bridgeSegmentClone = bridgeSegment:Clone()
		bridgeSegmentClone:PivotTo(startingPoint.CFrame)
		-- bridgeSegmentClone.Start:Destroy()
		-- bridgeSegmentClone.End.Transparency = 1

		-- Tween the bridge segment clone upwards
		local targetPosition = bridgeSegmentClone.PrimaryPart.Position + Vector3.new(0, 10, 0) -- Adjust the Y offset as desired
		local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
		local tween = TweenService:Create(bridgeSegmentClone.PrimaryPart, tweenInfo, {Position = targetPosition})
		tween:Play()

		startingPoint:Destroy()
		startingPoint = bridgeSegmentClone.End

		bridgeSegmentClone.Parent = workspace

		bridges += 1
	end
end)

1 Like

image
this only tweens the part the bridge attaches to (startPart)
same result if the bridge is welded or completely anchored

https://devforum.roblox.com/t/introduction-to-tweening-models/315253
This post could help you.

Some key takeaways from that post:

  • You will be tweening the model’s primary part
  • The primary part will be anchored while every other part will be unanchored
  • Every unanchored part must be welded to the primary part by using weld constraints

Here is some example code:

local TweenService = game:GetService("TweenService")
local bridgeSegment = script:WaitForChild("BridgeSegment")
local startingPoint = workspace:WaitForChild("Start")

local function CreateBridge()
	local bridgeSegmentClone = bridgeSegment:Clone()
	local primaryPart = bridgeSegmentClone.PrimaryPart
	
	--Change the Vector3 to change the starting position of the bridge
	primaryPart.CFrame = startingPoint.CFrame + Vector3.new(0, -10, 0)
	
	local finalState = {
		CFrame = startingPoint.CFrame
	}
	
	--Change the TweenInfo to affect how the bridge moves into place
	local tweenInfo = TweenInfo.new(1, Enum.EasingStyle.Linear, Enum.EasingDirection.Out)
	local tween = TweenService:Create(primaryPart, tweenInfo, finalState)
	tween:Play()
	
	startingPoint:Destroy()
	startingPoint = bridgeSegmentClone.End
	
	bridgeSegmentClone.Parent = workspace
	bridges += 1
end

Please keep in mind that every part other than the model’s primary part must be unanchored and welded to the primary part. Also feel free to change up the TweenInfo as you’d like. https://create.roblox.com/docs/reference/engine/datatypes/TweenInfo
Here is the documentation for TweenInfo.

I hope this helps. If there are any other problems, I can address them (whether I wrote my example code wrong or anything else).

1 Like

thank you! and i will be taking a look at the guide later on

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