So, I have this ViewPortFrame to show object and the Camera it contains spins using RunService
(RenderStepped). I want it to disconnect as soon as new function will call from the same function.
local isRendering = false
local Rotation = 0
function module.UpdateCharacterModelPreview(CharacterName, OutfitNumber, PreviewImageFrame, CharactersFolder)
local RenderStepped
local PreviewCamera = Instance.new("Camera", script)
PreviewCamera.Name = "PreviewCamera"
PreviewCamera.CameraType = Enum.CameraType.Scriptable
local PreviewSubject
local ViewportPoint = Vector3.new(0,0,0)
local ViewportPreview = PreviewImageFrame:WaitForChild("ViewportPreview")
--// Delete the current preview subject
for _, content in pairs(ViewportPreview:GetChildren()) do
if not content:IsA("UICorner") then
content:Destroy()
end
end
--// Subject
local function findSubject()
for _, CharFolder in pairs(CharactersFolder:GetDescendants()) do
if string.match(string.lower(CharacterName), string.lower(CharFolder.Name)) and CharFolder:IsA("Folder") then
print(CharacterName, "found!")
return CharFolder
end
end
end
--// Get outfit number
local function getCharTable()
for _, CharTable in pairs(Characters) do
if string.match(CharacterName, CharTable["Name"]) then
return CharTable
end
end
end
local OutfitName = getCharTable()["Context"]["Outfits"][OutfitNumber]
local SubjectFolder = findSubject()
for _, children in pairs(SubjectFolder:GetChildren()) do
if string.match(OutfitName, children.Name) then
local newChildren = children:Clone()
PreviewSubject = newChildren
break
end
end
ViewportPreview.CurrentCamera = PreviewCamera
PreviewSubject:SetPrimaryPartCFrame(CFrame.new(ViewportPoint))
PreviewSubject.Parent = ViewportPreview
local cframe, size = PreviewSubject:GetBoundingBox()
local Max = math.max(size.X, size.Y, size.Z)
local Distance = (Max/math.tan(math.rad(PreviewCamera.FieldOfView))) * 1.5
local CurrentDistance = (Max/2) + Distance
RenderStepped = game:GetService("RunService").RenderStepped:Connect(function()
PreviewCamera.CFrame = CFrame.Angles(0, math.rad(Rotation), 0) * CFrame.new(ViewportPoint + Vector3.new(0,0,CurrentDistance), ViewportPoint)
Rotation += 1
isRendering = true
end)
end
I’ve tried putting RenderStepped:Disconnect()
from the top but the output says error because nil.
but when I put it below the RenderStepped
it will stop the RunService.
Whats the best way to disconnect a RunService here?