Main menu screen randomly not working for some reason

i found this youtube tutorial about how to set up a main menu with the camera moved to a certain position. the code works, but for some reason, it only works sometimes. other times, the menu doesnt run, and the camera is on the player like normal. why is this happening and how do i fix it? it seems to be happening randomly

code:

--code mostly taken from: https://www.youtube.com/watch?v=ecyKTDFOl5g&ab_channel=DevFlare
--changes made by me
local CurrentCamera = workspace.CurrentCamera --get local players camera
local CameraPart = workspace:WaitForChild("menucamerapart") --get the camera part
local PlayButton = script.Parent.Frame.PlayButton --get the actual play button on the frame
local Humanoid = game.Players.LocalPlayer.Character:WaitForChild("Humanoid") --get local player's humanoid
local Frame = script.Parent.Frame --get main menu frame
local TPPart = workspace.tppad --location to tp player after clicking play
wait(0.001) --no clue why this is here tbh

CurrentCamera.CameraType = Enum.CameraType.Scriptable --allow camera to be controlled by scripts
CurrentCamera.CFrame = CameraPart.CFrame --set camera's cframe to the cframe of the camera part
Frame.Visible = true --make frame visible (just easy to work on studio with this, dont gotta hide ui n stuff)
Humanoid.WalkSpeed = 0 --player cant move when main menu is on
Humanoid.JumpPower = 0 --player cant jump when main menu is on
game.StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

function Play()
	CurrentCamera.CameraType = Enum.CameraType.Custom --set camera back to default settings
	Humanoid.WalkSpeed = 24 --let player move
	Humanoid.JumpPower = 50 --let player jump
	Humanoid.Parent:MoveTo(TPPart.Position) --tp player on the map from spawn box
	script.Parent:Destroy() --remove the main menu
end

PlayButton.MouseButton1Click:Connect(Play) --run it
1 Like

idk your local script probably runs before your Camera could load, try adding print statements to debug your code so you can identify the issue easily. Are there any errors in the Console?

1 Like

there is an error in the console:


not sure why its saying “tppad isnt a member of workspace” when its literally right there

local TPPart = workspace:WaitForChild("tppad")

2 Likes

Mark this as solution so he gets stats
@playerpack12345

2 Likes

I’m not sure if you understand the error so here’s an explanation:

The TPPart is on the Server, when you join as a Player you receive the data from the Server but it takes time for the Data to load. This is what we call Server to Client replication. So in this instance by using :WaitForChild() you essentialy wait until your Device has loaded the Part.

1 Like

thanks for the explanation man

i keep seeing “waitforchild” n stuff like that in scripting tutorials on youtube and im always confused on why its necessary, it finally makes sense now lol

1 Like

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