What I’m trying to do here, is make the camera spin around a car in a ViewportFrame, however every time I select a different car, and while this Technically works, this happens;
Code:
local CarSpawn = {}
local function rotateCar(carPart,Viewport)
local rotation = 0;
game:GetService("RunService").Heartbeat:Connect(function()
local CFrame_1 = carPart.CFrame * CFrame.Angles(0, math.rad(rotation), 0) * CFrame.new(-10, 7, -10)
CFrame_1 = CFrame.new(CFrame_1.p, carPart.Position)
Viewport.CurrentCamera.CFrame = CFrame_1
rotation = rotation + .5
end)
end
function CarSpawn.showCarInfo(CarInfo, CarName, Price, Type)
local Viewport = CarInfo.ViewportFrame
local clonedCar = game.ReplicatedStorage.Cars[Type][CarName]:Clone()
clonedCar.Parent = Viewport
local carPart = clonedCar.PrimaryPart
local Camera = Instance.new("Camera")
Camera.Parent = Viewport
Viewport.CurrentCamera = Camera
for i,v in pairs(carPart:GetChildren()) do
v:Destroy()
end
end
return CarSpawn
Still having trouble with this, this is how I have the connections set up but I’m obviously very new to them, so it’s not quite working;
Error: Players.anthlons.PlayerGui.MainUI.Client:101: attempt to index nil with 'Disconnect' - Client - Client:101
local connection
for i,v in pairs(carSpawnFrame.ScrollingFrame:GetChildren()) do
if v:IsA('TextButton') then
v.MouseButton1Click:Connect(function()
connection:Disconnect()
local carPart = clonedCar.PrimaryPart
local Camera = Instance.new("Camera")
Camera.Parent = carInfo.ViewportFrame
carInfo.ViewportFrame.CurrentCamera = Camera
for i,v in pairs(carPart:GetChildren()) do
v:Destroy()
end
local rotation = 0;
connection = game:GetService("RunService").Heartbeat:Connect(function()
local CFrame_1 = carPart.CFrame * CFrame.Angles(0, math.rad(rotation), 0) * CFrame.new(-10, 7, -10)
CFrame_1 = CFrame.new(CFrame_1.p, carPart.Position)
carInfo.ViewportFrame.CurrentCamera.CFrame = CFrame_1
rotation = rotation + .5
end)
end)
end
end