Attemp to index nil with anchored

  1. What do you want to achieve? Keep it simple and clear!
    A grab and throw script

  2. What is the issue? Include screenshots / videos if possible!
    Attempt to index nil with anchored in line 20.

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    Setting a random variable with an if else.

local player = game.Players.LocalPlayer 
local mouse = player:GetMouse() 
local target 
local down 
local cam = workspace.Camera
local offset 

mouse.Button1Down:connect(function() 
	offset = math.abs((cam.CFrame.Position-mouse.Hit.Position).Magnitude)
	if mouse.Target ~= nil and mouse.Target.Locked == false then 
		target = mouse.Target 
		mouse.TargetFilter = target 
		down = true
	end 
	print(offset)
end)

mouse.Move:Connect(function()
	if not target.anchored then
		if down == true and target ~= nil then 
			target.Position = cam.CFrame.Position-(mouse.Hit.Position - cam.CFrame.Position).Unit*-offset
		end 
	end
end) 

mouse.Button1Up:connect(function()
	down = false 
	mouse.TargetFilter = nil 
	target = nil
end)

game:GetService("RunService").RenderStepped:Connect(function(dt)
end)

Do this

local target = mouse.Target
if target  and not target.Anchored then

Target is nil so its doing nil.Achored

1 Like