[SOLVED THE PROBLEM, CHECK BOTTOM FOR SOLUTION]
Hello all programmers. I am having a problem with a main menu script. When I playtest, I get an error that says " Workspace.SemperGratis.MainMenuScript:13: attempt to index nil with ‘Position’ ".
This is what it looked like prior to this error:
Here is what it looks like after the error:
Here is the code:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local Camera = workspace.CurrentCamera
local RunService = game:GetService("RunService")
local RS = game:GetService("ReplicatedStorage")
local camPart = RS:FindFirstChild("camPart"):Clone()
local inMenu = true
task.wait(0.2)
camPart.Parent = workspace
camPart.Position = workspace:FindFirstChild("introPart").Position
Camera.CameraSubject = camPart
Camera.CameraType = Enum.CameraType.Scriptable
local moveFactor = 0.0005
local previousMouseX, previousMouseY = nil, nil
local MAX_POSITION = 0.5
local OriginalPosition = camPart.Position
local UI = RS:FindFirstChild("MenuUI"):Clone()
UI.Parent = workspace
local targetPosition = OriginalPosition
local moving = false
local PlayButton = UI:FindFirstChild("PlayPart").SurfaceGui.TextButton
PlayButton.Activated:Connect(function()
UI:Destroy()
camPart:Destroy()
Camera.CameraSubject = player.Character:FindFirstChild("Humanoid")
Camera.CameraType = Enum.CameraType.Custom
inMenu = false
end)
RunService.RenderStepped:Connect(function()
if inMenu == false then return end
local mouse = player:GetMouse()
local screenX = Camera.ViewportSize.X
local screenY = Camera.ViewportSize.Y
local leftoverScreenX = screenX - mouse.X
local leftoverScreenY = screenY - mouse.Y
local DistanceFromOriginX = (screenX/2) - leftoverScreenX
local DistanceFromOriginY = leftoverScreenY - (screenY/2)
local clampedX = math.clamp(DistanceFromOriginX * moveFactor, -MAX_POSITION, MAX_POSITION)
local clampedY = math.clamp(DistanceFromOriginY * moveFactor, -MAX_POSITION, MAX_POSITION)
if previousMouseX and previousMouseY then
local mouseMoved = (DistanceFromOriginX ~= previousMouseX) or (DistanceFromOriginY ~= previousMouseY)
if mouseMoved then
moving = true
targetPosition = OriginalPosition + Vector3.new(clampedX, clampedY, 0)
else
moving = false
end
end
if moving then
camPart.CFrame = camPart.CFrame:Lerp(CFrame.new(targetPosition), .1)
else
camPart.CFrame = camPart.CFrame:Lerp(CFrame.new(targetPosition), .2)
end
local OffsetUIX = (screenX/4) * 0.009
local OffsetUIY = (screenY/4) * 0.009
Camera.CFrame = Camera.CFrame:Lerp(CFrame.new(camPart.Position), .2)
local finalTargetPosition = camPart.Position + Vector3.new(-OffsetUIX, -OffsetUIY+1, -10)
if UI.PrimaryPart then
UI:SetPrimaryPartCFrame(UI.PrimaryPart.CFrame:Lerp(CFrame.new(finalTargetPosition) * CFrame.Angles(0, math.rad(23), 0), .1))
end
previousMouseX = DistanceFromOriginX
previousMouseY = DistanceFromOriginY
end)
(please don’t judge me if the code is bad, I followed a tutorial on this)
I believe part of the issue might be that I added in a StarterCharacter, or that the camPart is not loaded in before it is set as the camera subject, but I am unsure.
If you know how to fix this, please respond in the comments.
Have a great day everyone!
[SOLUTION: I used WaitForChild instead of FindFirstChild. All I did.]