luascriptingroblox

Part can't be found by local script in Roblox


I have a Tween service that moves the camera between two parts. However using a direct call such as game.Workspace.cameras.spawnselection.default_camera to get the parts fails.

Here is my code:

--Local Script
local TweenService=game:GetService("TweenService")
local camera=game.Workspace.Camera
local cameras=game.Workspace:WaitForChild("cameras")
local play=script.Parent["main menu"]["main frame"].PlayButton


--Camera Locations
local starting_camera=cameras:WaitForChild("spawnselection"):WaitForChild("default camera")
local hillcrest_camera=cameras.spawnselection:WaitForChild(""):WaitForChild("Hillcrest")



local function createtween(end_:CFrame,time_:number)
    local cutscene=TweenService:Create(camera,TweenInfo.new(time_),{CFrame=end_})
    return cutscene
end

play.Activated:Connect(function()
    camera.CameraType=Enum.CameraType.Scriptable
    local selection
    local canswitch=true
    local cutscene=createtween(hillcrest_camera.CFrame,10)
    
    
    if canswitch then
        camera.CFrame=starting_camera.CFrame
        cutscene:Play()
        canswitch=false 
    end
    
    cutscene.Completed:Connect(function()
        canswitch=true
    end)
    
    
    
end)

I tried using a ':WaitForChild' method, but this obviously doesn't work, due to the warning: Infinite yield possible on 'Workspace.cameras.spawnselection:WaitForChild("default camera"). I was expecting this to work first-try. I then ran a server script to get the Parents of the parts and this returned the correct values, and the local script worked once. However changing just the transperncy of the parts broke the LocalScript. The objects show during run-time in the explorer, i am sure i am acessing the right objects, as i can view them via a server script. Can A Local Script Not Access Their OWN Copy Of The Workspace? EDIT: Here is my place. The problomatic file is in StarterGUI and the LocalScripts' name is SpawnSelection.


Solution

  • Infinite spawnselection Yield

    Workspace.cameras.spawnselection does not exist on the workspace nor does a script ever create it, so yielding to it will always result in an infinite yield creating your error.

    I removed all the references to Workspace.cameras.spawnselection and replaced them with Workspace.cameras which is a valid location. I chose this since there are instances which the :WaitForChild().

    The corrected version is this but it still has an error in it:

    local starting_camera=cameras:WaitForChild("default camera")
    local hillcrest_camera=cameras:WaitForChild("Hillcrest")
    

    Infinite default camera Yield

    The next error is for default camera. There are no instances named default camera or default_camera in the workspace and no scripts create it so, like before, yielding to it will cause an infinite yield error.

    I noticed there was a Part named start_camera in Workspace.cameras which is likely what you wanted to use, given the variable name.

    local starting_camera=cameras:WaitForChild("start_camera")
    

    Instance Streaming

    I noticed another issue with Instance Streaming causing the parts the script uses to be streamed out. They are two fixes for this.

    1. Adding to your script to request area streaming for the parts
    2. Ensure the player spawns close enough to the parts to be within the StreamingMinRadius so they are always loaded
    3. Disable Instance Streaming by setting Workspace.StreamingEnabled to false

    Conclusions

    Your script specified the incorrect locations for Parts and the Parts were being streamed out. These caused the errors you would be experiencing.

    Your final fixed script would be this:

    --Local Script
    local TweenService=game:GetService("TweenService")
    local camera=game.Workspace.Camera
    local cameras=game.Workspace:WaitForChild("cameras")
    local play=script.Parent["main menu"]["main frame"].PlayButton
    
    
    --Camera Locations
    local starting_camera=cameras:WaitForChild("start_camera")
    local hillcrest_camera=cameras:WaitForChild("Hillcrest")
    
    
    
    local function createtween(end_:CFrame,time_:number)
        local cutscene=TweenService:Create(camera,TweenInfo.new(time_),{CFrame=end_})
        return cutscene
    end
    
    play.Activated:Connect(function()
        camera.CameraType=Enum.CameraType.Scriptable
        local selection
        local canswitch=true
        local cutscene=createtween(hillcrest_camera.CFrame,10)
        
        
        if canswitch then
            camera.CFrame=starting_camera.CFrame
            cutscene:Play()
            canswitch=false 
        end
        
        cutscene.Completed:Connect(function()
            canswitch=true
        end)
        
        
        
    end)
    

    Make sure to refer to the Instance Streaming section as well to make sure that your parts properly are loaded if you are still encountering issues.

    Place File & Video

    Here is your place file with the fixes implemented: https://drive.google.com/file/d/1KSdpoa_S4aD-eQOr_WMtcxzBHVcTz3lZ/view?usp=sharing

    Note I used option 3 for solving the Instance Streaming problem but you should use option 1.

    Video of the fixed version: https://drive.google.com/file/d/1geXRDi22DsIGguf0BzMWwWhfC_yOEdD8/view?usp=sharing