Tween a Model to a Certain Point

Currently, I am creating a pet system where every frame my gui is set to the parts anchor or eggs anchor as I could say. The model should only be tweening up by 5 studs, but since this runs every frame, it will keep going regardless. How can I stop the egg at a certain poisiton, then bring it back?

local UIS = game:GetService("UserInputService")
local RS = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local RemoteEvents = ReplicatedStorage.RemoteEvents
local Player = game.Players.LocalPlayer
local Camera = workspace.CurrentCamera
local DefaultSize = script.Parent.Size

function determineClosest(EggsAvailable)
	local CurrentClosest = nil
	local ClosestDistance = script.Parent.MaxMagnitude.Value
	for i,v in pairs(EggsAvailable) do
		local Egg = workspace.Eggs:FindFirstChild(v)
		local mag = (Egg.UIanchor.Position-Player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
		if mag <= ClosestDistance then
			CurrentClosest = Egg
			ClosestDistance = mag
		end
	end
	return CurrentClosest
end

RS.RenderStepped:Connect(function()
	if Player.Character:FindFirstChild("Humanoid") then
		if Player.Character.Humanoid.Health ~= 0 then
			local EggsAvailable = {}
			local CameraRatio = ((Camera.CFrame.Position - Camera.Focus.Position).Magnitude)/11
			script.Parent.Visible = false
			for i,v in pairs(game.Workspace.Eggs:GetChildren()) do
				local mag = (v.UIAnchor.Position-Player.Character:WaitForChild("HumanoidRootPart").Position).Magnitude
				if mag <= script.Parent.MaxMagnitude.Value then
					EggsAvailable[#EggsAvailable+1] = v.Name
				end
			end
			if #EggsAvailable == 1 then
				local Egg = workspace.Eggs:FindFirstChild(EggsAvailable[1])
				local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(Egg.UIAnchor.Position)
				script.Parent.Visible = true
				script.Parent.Position = UDim2.new(0,WSP.X,0,WSP.Y)
				script.Parent.CurrentTarget.Value = Egg.Name
				for i,v in pairs(workspace.Eggs:GetChildren()) do
					if v.Name ~= Egg.Name then
						v.Outline.Color = Color3.fromRGB(255,255,255)
						local primaryEgg = v.EggModel.PrimaryPart
						local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, true, 0)
						local Tween = game:GetService('TweenService'):Create(primaryEgg, tweenInfo, {Position = primaryEgg.Position + Vector3.new(0, -5, 0)})
						Tween:Play()
					else
						local primaryEgg = v.EggModel.PrimaryPart
						local tweenInfo = TweenInfo.new(0.2, Enum.EasingStyle.Sine, Enum.EasingDirection.InOut, 0, true, 0)
						local Tween = game:GetService('TweenService'):Create(primaryEgg, tweenInfo, {Position = primaryEgg.Position + Vector3.new(0, 5, 0)})				
						Tween:Play()
						v.Outline.Color = Color3.fromRGB(0,255,0)
					end
				end
			elseif #EggsAvailable > 1 then
				local Egg = determineClosest(EggsAvailable)
				local WSP = game.Workspace.CurrentCamera:WorldToScreenPoint(Egg.UIAnchor.Position)
				script.Parent.Visible = true
				script.Parent.Position = UDim2.new(0,WSP.X,0,WSP.Y)
				script.Parent.CurrentTarget.Value = Egg.Name
				for i,v in pairs(workspace.Eggs:GetChildren()) do
					if v.Name ~= Egg.Name then
						v.Outline.Color = Color3.fromRGB(255,255,255)
					else
						v.Outline.Color = Color3.fromRGB(0,255,0)
					end
				end
			elseif #EggsAvailable == 0 then
				script.Parent.CurrentTarget.Value = "None"
				for i,v in pairs(workspace.Eggs:GetChildren()) do
					v.Outline.Color = Color3.fromRGB(255,255,255)
				end
			end
			script.Parent.Size = UDim2.new(DefaultSize.X.Scale/CameraRatio, DefaultSize.X.Offset, DefaultSize.Y.Scale/CameraRatio, DefaultSize.Y.Offset)
		end
	end
end)
1 Like

TweenService. I did this with my pet system, though here is a quick warning: If you suck at cframes like me, this will take awhile to make. It took me over 4 days to finish. (This is only one of the lines)

tweenservice:Create(APet.PrimaryPart, TweenInfo.new(5,Enum.EasingStyle.Quint,Enum.EasingDirection.Out),{CFrame = APet.PrimaryPart.CFrame * CFrame.new(0,-13,0)}):Play()

This is an example of what you want to have. You have to create a custom tween and enter all of the args for it. The last arg is the only one you want to have to worry about, everything else is in the roblox api, TweenService | Roblox Creator Documentation. (APet var is the pets model, you also want to make sure you have nothing anchored except the humanoidrootpart. If it doesn’t work unanchor and anchor stuff; in short play around with it.)

If you have any questions, reply and tell me! :slight_smile:

I am not tweening the pets. I already have that accomplished. I am trying to make it so when I am in range of the egg model, the gui is visible (which works) and have the egg model float in the air.

I am very confused. What are you trying to do exactly? :confused:

You know in an egg system where when you get near the egg, a gui pops up? I already have that accomplish and that renders every frame. The problem is that I am trying to tween the egg model itself. The display portion. So basically, it tweens, but it does that tween for an indefinite time, until I step out of the range of the gui

Well after it tweens you could enable a boolean value inside of the player and that same script would pick it up and it would finish the script/tween.

I dont know that would be all that efficient to add a boolean since it could still override since I am rendering each frame and it could cause issues down the line.

Well it would be at the end of the script. What seems like i’m hearing is that the egg is constantly moving up and down. If so, if its not already in another function/loop like this make sure it is or you won’t be able to do anything without messing up or making it look ugly.

Model:MoveTo() was a function I just figured out. Maybe that could help you?

MoveTo is a function used for the humanoid I believe

Nope, it can also be used for models! It worked for me yesterday! Model:MoveTo(Vector3 pos)

Yeah you are right, just check the API. lol Anyways, so would I be able to get smooth movements with this or would the MoveTo function just be relative to the renderstepped event?

If you want smooth movements just use the tween post I made. MoveTo() isn’t smooth. You can run that in a renderstepped event. :confused:

The moveto function is super fast. So it doesn’t give the effect of tweening

1 Like

If you really don’t like using tweenservice you can always try lerps.

Oh no, I use tween service for most things anyways.

rippppp. I was afraid of that. Is there any other work around?

Hmmm… Have you tried the tween code? If so, what errors pop up?

There isnt any errors. Everything is work just fine is that when I do use the functions, the model moves forever because of the rendered stepped.

Then you can easily put that in the script. All you would have to do is make a count and plus that count by one once every tween is finished. If the count == 5 or x then you stop the gui/script and close the gui/camera or whatever you are using for your egg hatching system.