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)