Problem with Tweening in PartClone Movement

Hello, my issue is as follows: I have a tool, and when I equip the tool, a JellyFishClone is spawned in the workspace and starts following me from behind. However, I tried to add a tween to make it move slightly up and down. But, while attempting to create this tween, it interferes with the movement of the JellyFishClone. How can I resolve this situation? I want it to both follow me and have a slight up-and-down movement with the tween.

Video:

Code:

local Tool = script.Parent
local Handle = Tool.Handle

local Jellyne = Instance.new("Model")
Jellyne.Name = "JellyFish"

local player = Tool.Parent.Parent
if player.Character then
	player.Character:WaitForChild("Humanoid").Died:Connect(function()
		if game.Workspace:FindFirstChild(player.Name.." JellyFish Handle") then
			game.Workspace:FindFirstChild(player.Name.." JellyFish Handle"):Destroy()
		end
	end)
end
Tool.Equipped:Connect(function()
	if not game.Workspace:FindFirstChild(player.Name.." JellyFish Handle") then
		Tool.Enabled = false
		vCharacter = Tool.Parent
		myTorso = vCharacter:FindFirstChild("Torso") or vCharacter:FindFirstChild("UpperTorso")
		myHumanoid = vCharacter:FindFirstChild("Humanoid")
		playerName = myHumanoid.Parent

		if myTorso == nil or myHumanoid == nil then
			return
		end

		if Jellyne.Parent ~= nil and JellyneClone then
			local target = myHumanoid.TargetPoint
			local direction = (target - JellyneClone.Position).unit
		else
			JellyneClone = Handle:Clone()
			JellyneClone.Name = "JellyFish"
			JellyneClone.Parent = Jellyne

			local floatForce = Instance.new("BodyForce")
			floatForce.force = Vector3.new(0, getMassOfObject(Tool) * workspace.Gravity, 0)
			floatForce.Name = "Float"
			floatForce.Parent = JellyneClone

			local bv = Instance.new("BodyVelocity")
			bv.Name = "FollowBV"
			bv.Parent = JellyneClone
			bv.maxForce = Vector3.new(50000, 50000, 50000)
			bv.P = 50000


			Jellyne.Parent = workspace  -- Değişiklik: Parent'ı workspace olarak ayarlandı
			Jellyne.Name = playerName.Name.." JellyFish Handle"  -- Değişiklik: Modelin adı değiştirildi

			Jellyne:MoveTo(myTorso.Position + myTorso.CFrame.lookVector * 5.0)
			setTransparency(1)

			coroutine.resume(coroutine.create(follow))
		end
	end
end)
function getMassOfObject(obj)
	local children 
	local mass = 0
	if type(obj) == "userdata" then 		
		children = obj:GetChildren() 	 
	elseif type(obj) == "table" then 
		children = obj 
	end 
	if children then 
		for i = 1, #children do 
			if children[i]:IsA("BasePart") then 
				mass = mass + children[i]:GetMass()
			end 
			if #children[i]:GetChildren() > 1 then 
				mass = mass + getMassOfObject(children[i])
			end 
		end 
	end  	
	return mass
end 

function setTransparency(value)
	if value == nil then value = 0 end 
	Handle.Transparency = value 
	local children = Handle:GetChildren()
	for i = 1, #children do
		if children[i] and children[i]:IsA("BasePart") then 
			children[i].Transparency =  value
		end 
	end 
end 

function follow()     
	local bv = JellyneClone:FindFirstChild("FollowBV")
	local stopped = false
	while wait() do
		local distance = (myTorso.Position - JellyneClone.Position).magnitude

		if distance > 5.0 then      
			stopped = false
			JellyneClone.CFrame = CFrame.new(JellyneClone.Position, myTorso.Position + Vector3.new(0, JellyneClone.Position.Y - myTorso.Position.Y, 0))
			if distance > 8.0 then
				bv.velocity = (myTorso.Position - JellyneClone.Position).unit * 20.0 + Vector3.new(0, 2, 0)
			else
				bv.velocity = (myTorso.Position - JellyneClone.Position).unit * 20.0 * (distance/24) + Vector3.new(0, 2, 0)
			end
		else                
			if not stopped then
				stopped = true
				bv.velocity = Vector3.new(0, 0, 0)
				JellyneClone.Velocity = Vector3.new(0, 0, 0)
			end
		end
	end
end
1 Like

Which part of the code handles the tween you’re trying to do?

I tried this

image

local TweenService = game:GetService("TweenService")
local part = script.Parent:WaitForChild("FollowBV")

local function startTween()
	local posPart = part.Parent.Position
	local info = TweenInfo.new(
		1, -- Kaç saniyede gerçekleşeceği
		Enum.EasingStyle.Linear,
		Enum.EasingDirection.InOut,
		-1, -- Kaç kez tekrarlanacağı (-1 sonsuz döngü olur)
		true, -- Döngü ters mi, düz mü
		0 -- Vardığında, kaç sn bekleyecek ve tekrar dönücek
	)

	local tween = TweenService:Create(part.Parent, info, {
		Position = posPart + Vector3.new(0, 0.7, 0)
	})

	tween:Play()
end

local function onPartChanged()
	if part.Velocity == Vector3.new(0, 0, 0) then
		wait(1.5)
		if part.Velocity == Vector3.new(0, 0, 0) then
			startTween()
		end
	end
end

part.Changed:Connect(onPartChanged)

But it didn’t work because it happens like in the video:

1 Like

I think what you could do given that you want to move your jellyfish up and down, you could use sine waves to achieve that

There’s some posts on the devforum that tackle something similar to yours

1 Like

Thank you, but they say it needs to be used in a LocalScript for RenderStepped , but it should be in a normal script. That’s why I still don’t know what to do

I think it should still be possible to make it work through regular scripts, RenderStepped is one way to get a loop going

2 Likes

I don’t think you can with tweens, you’d need to just program in the movement using CFrames. In that case you can just use keep an index, add 1 every frame, and then plug it into math.sine. The result is the offset (relative to the median height) that should be applied to the model.

1 Like

It should definitely be on a local script. Why would you have the server calculate something like that every frame, if it’s not important for replication? Clients should be calculating that.

yes, i tried it. but it was still the same, I couldn’t do it.

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