Script working in-game but not when testing in studio

Basically, I’m attempting to make a menu scene with tweening cameras. When I test it in studio, it only works half the time and now its stopped working in studio but when I play the game on roblox normally it works. (Not a problem with publishing etc, the game is the same as whats in studio). The folder does exist in the workspace.

I constantly get this in the output:

MenuCams folder is missing or empty! - Client - CinematicPlayer:12

I’m not the smartest when it comes to scripting, but I’m trying.

Script
local TweenService = game:GetService("TweenService")
local Players = game:GetService("Players")
local Lighting = game.Lighting
local Camera = workspace.CurrentCamera


Camera.CameraType = Enum.CameraType.Scriptable


local folder = workspace:FindFirstChild("MenuCams")
if not folder or #folder:GetChildren() == 0 then
	warn("MenuCams folder is missing or empty!")
	return
end


local parts = folder:GetChildren()
table.sort(parts, function(a, b)
	return tonumber(a.Name) < tonumber(b.Name)
end)


local initialCamera = folder:FindFirstChild("6")
if initialCamera then
	Camera.CFrame = initialCamera.CFrame
else
	warn("Camera 6 not found in MenuCams!")
	return
end


Lighting.ClockTime = 3.9

for i = 6, 1, -1 do
	local targetPart = folder:FindFirstChild(tostring(i))
	if targetPart and targetPart:IsA("BasePart") then
		local tweenInfo = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
		local tween = TweenService:Create(Camera, tweenInfo, {CFrame = targetPart.CFrame})
		tween:Play()
		tween.Completed:Wait()
	else
		warn("Camera " .. i .. " is missing or invalid. Skipping.")
	end
end

-- Set final camera to MouseCam and enable parallax effect
local mouseCam = workspace.MenuArea:FindFirstChild("MouseCam")
if not mouseCam then
	warn("MouseCam not found in MenuArea!")
	return
end

Camera.CFrame = mouseCam.CFrame


local player = Players.LocalPlayer
local mouse = player:GetMouse()
local parallaxStrength = 1 

mouse.Move:Connect(function()
	local offsetX = mouse.X / Camera.ViewportSize.X
	local offsetY = mouse.Y / Camera.ViewportSize.Y

	local newCFrame = mouseCam.CFrame:ToWorldSpace(
		CFrame.new(offsetX * parallaxStrength, offsetY * parallaxStrength, 0)
	)
	Camera.CFrame = newCFrame
end)


local mainMenu = player.PlayerGui.MenuGui.Frame.ButtonHolder
local targetPosition = UDim2.new(0.333, 0, 0.656, 0) 

local menuTweenInfo = TweenInfo.new(1, Enum.EasingStyle.Quad, Enum.EasingDirection.Out)
local menuTween = TweenService:Create(mainMenu, menuTweenInfo, {Position = targetPosition})
menuTween:Play()

Any help is welcome!

Try using WaitForChild instead of FindFirstChild when accessing the folder.

Same issue, still getting the same error

Try printing each Parent of the folder’s location in order to see which isn’t there when the script is checking.

Then it’s probably the children not getting replicated quick enough for the script to be able to count them, and it working just sometimes proves that, cause replicating speeds constantly change based on a lot of factors
You’d have to in some way wait for the children to replicate before making the if statement

1 Like

This didn’t cross my mind, thank you! I added task.wait() before the if statement.

1 Like

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