#Error attempt to index nil with Parent

1 messages · Page 1 of 1 (latest)

tawdry canopy
#

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);
fallen current
#

Here are the main issues in your script:
1. target isn’t initialized
You use table.find(target, descendant) and table.insert(target, descendant) before target is guaranteed to exist.
2. grabbedObject might be nil
You index grabbedObject[1] immediately when it’s set, which will error if grabbedObject wasn’t stored properly.

tawdry canopy
#

So apparently grabbedObject exists but it's nothing somehow

fallen current
#

Initialize target and make sure grabbedObject is set safely with :waitforchild()

#

Or other methods

#

Holdup

#

Idk

tawdry canopy
#

Yea it's really weird I've never ever gotten an error with target which existed before I ever added grabs into it

#

So I'm almost certain target is fine because it's supposed to be nil sometimes if that makes sense but the way my code processes in my brain is that grabbedObject should completely ignore target if it's nil

fallen current
#

Why don’t you just use Roblox’s ‘drag detector’ item

#

It lets you drag stuff

tawdry canopy
#

...

#

That exists?

fallen current
#

yes

tawdry canopy
#

If you mean the dragattachment I gave up on that since I couldn't get it to work

fallen current
#

just make it a child of a part

tawdry canopy
#

Oh I think I remember using that once actually I don't think it'll work bc I spawn the items in the game

#

Plus I'm grabbing stuff from the center of the screen so it works with mobile

#

Unless DragDetector can work with that?

fallen current
#

Pretty sure it does

#

It’s official Roblox