The code works but when the player spawns it spams with attempt to index nil until you press the button to grab
Players.LocalPlayer.PlayerGui.ScreenGui.MobileButtons.Unstore.MouseButton1Click:Connect(function(input, processed)
if target and not grabbedObject then
grabbedObject = target
else
DropObject()
end
end)
RunService.Heartbeat:Connect(function()
if grabbedObject then
local targetCF = workspace.CurrentCamera.CFrame * CFrame.new(0,0, -distance)
Id.CFrame = targetCF
local model = grabbedObject[1].Parent
local orientation, size = model:GetBoundingBox()
local ome = orientation - orientation.Position
local NewPos = CFrame.new(Id.Position) * ome
grabbedObject[1].Parent:PivotTo(NewPos)
DragPosUpdate:FireServer(grabbedObject, NewPos)
HighlightObject(grabbedObject, true)
else
local ray = workspace.CurrentCamera:ViewportPointToRay(screenSize.X/2, screenSize.Y/1.62)
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Exclude
raycastParams.FilterDescendantsInstances = player.Character:GetDescendants()
local result = workspace:Raycast(ray.Origin, ray.Direction * MAX_DISTANCE, raycastParams)
if result and result.Instance and result.Instance:IsDescendantOf(workspace.Draggable) and result.Instance.Parent:IsA("Model") then
for _, descendant in pairs(result.Instance.Parent:GetDescendants()) do
if descendant:IsA("BasePart") and not table.find(target, descendant) then
table.insert(target, descendant);