How to "Tween" Weld Attachment

Hey everyone! I’m trying to make a carrying object system. I’ve done some of it using welds and it works quite well, but I would like to improve it, adding a kind of Tween to when the object is picked, so it goes smoothly to the player’s position.

local Player = game.Players.LocalPlayer
local Character = Player.Character or Player.CharacterAdded:Wait()

local Click = pickable.MouseClick:Connect(function()
			if Character.PlayerInfo.WeightCarrying.MaxWeight.Value == false then
				--Stuff
				pickable.Parent.Anchored = false
				pickable.Parent.CanCollide = false
				pickable.MaxActivationDistance = 0
				wait()
				local OffsetLight = Vector3.new(1, -1, -1)
				local OffsetHeavy = Vector3.new(0, -1, -1)
				local Weld = Instance.new("Weld")

				Weld.Parent = pickable.Parent
				wait()
				Weld.Part0 = pickable.Parent
				Weld.Part1 = Character.Head

Now, I’m almost 100% sure that you can’t tween a weld like the way I’m trying, but anything that makes it goes to the welded position smoothly would be highly appreciated!

could you try tweening pickable and then welding it?

1 Like

Yeah, but how to make it work properly, considering that the player can click to pick up something and then continue walking. His camera and body positions could be completely different from where the tween was originally set to.

1 Like

Good news is i know how, bad news is youre gonna have problems with network, replicating the position of the object if ure using welds.

But here is a script for a custom tween so u can tween whatever you want:


local function lerp(a, b, t)
	return a + (b-a) * t
end

local RS = game:GetService("RunService")
local t = 0
local tween
local PartOriginalPosition = Part.Position
tween = RS.Heartbeat:Connect(function(delta)
    t += delta
    -- t going from 0 to 1, you can use it to lerp something like

    Part.Position = lerp(PartOriginalPosition, goalPosition, t)

    if t >= 1 then
    -- when tween ends
        if tween then tween:Disconnect() tween = nil end
    end
end)

If you want to add EasingStyle effects on this tween, you can add TweenService:GetValue(t)

2 Likes

Cool, going to try it! But what do you mean when you say “problems with network” ?

When you are using a Accessory, it automatically sets the accessories part’s network ownership to the player, that does not happens if you are just welding a part in your character, you might see the model move after your character in the game, you must try to give the part’s network ownership to the player on the server. You cant set network ownership if the part is anchored or welded to anchored parts though, and you can only do it on server side (so your character can define the position of the part with the weld), which makes this feature kindda hard to use. Good luck with that.

3 Likes

Also, im considering you discarted the use of TweenService:Create() at all, if you didnt, you might want to look around this instead

Using this Create function will not allow you to tween to a moving goal though.

Thank you for the code and the explanation! Since it’s a first person singleplayer game I think I can try to avoid it for now.

Yeah, it seems better, as I could also use the same system again in the future without the need of fixing this problem, but as I said, I got no idea on how to work around the problems that may happen, like this one:

But maybe in the future I will look further around this

What is supposed to be goalPosition?

I’m assuming it would be the player’s position because as far as I can see from your original post, you want the object to tween to the player’s position.

I think it’s supposed to be assigned within the RunService function so that it can keep track of the player’s position.

1 Like

Oh, well I arleady tried something like that using actual tweens and then welding the object, and it sucks, so I guess I’m gonna to give up on the idea and just add a cool sound effect.

You can tween a weld using game:GetService("TweenService") and tweening the C0 or C1 Properties:

local Weld = Instance.new("Weld")
local twService = game:GetService("TweenService")
local pos = Vector3.new(0,0,0)
local rotation = Vector3.new(0,0,0)
twService:Create(Weld,TweenInfo.new(1),{C0=CFrame.new(pos,rotation)}):Play()
1 Like

I think it has 1 hidden property called Transform so you can apply a cframe to it and it will not change C0 or C1.
you should apply transform on RunService.Stepped events because if you have the default animator it will override it.

1 Like

on this tween I gave you, you have the ability to use a moving goal Position. The function inside the heartbeat event can aways get the current position of your destiny, instead of having only the position when you begin the tween.

@fares14213 gave a nice idea too, normal tweening the C0 would also create the same effect, working with the welded parts’ movement.

Here is an exemple with the script i gave:

local function lerp(a, b, t)
	return a + (b-a) * t
end

local Part = script.Parent.Part
local Part2 = script.Parent.Part2

local RS = game:GetService("RunService")
local t = 0
local tween
local PartOriginalPosition = Part.Position
tween = RS.Heartbeat:Connect(function(delta)
	t += delta/5 -- division will slow the time it takes from 0 to 1
	-- t going from 0 to 1, you can use it to lerp something like

	Part.Position = lerp(PartOriginalPosition, Part2.Position, t)

	if t >= 1 then
		-- when tween ends
		if tween then tween:Disconnect() tween = nil end
	end
end)

while tween do
	Part2.Position += Vector3.new(0,0.1,0)
	task.wait()
end
1 Like