Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Detecting if there's something on mouse position?

So i got a dragging system it's a localscript i put it on player starterpack, it uses setprimarypartcframe to move the model, the problem is the model can be placed to another models it's like its cancollide even tho the parts itself is not, so i was told to use region3 so i did but there's problem..


This is only part of my dragging script on starterpack, which when a player released the button assuming the player is dragging the cloned model it's supposed to checked if there is parts on the area, if there is none it'll print no parts and if there is then it'll print "parts".

THE PROBLEM

it prints parts so it detects but whenever i move it to area where there is no parts it says '23:40:52.528 - Players.Player1.Backpack.mouseTargeting:62: attempt to index local 'd' (a nil value)'

mouse.Button1Up:connect(function()
while wait() do
local ignorep = mouse.Target.Parent
local d = mouse.TargetFilter
local pos = d.Position
local size = d.Size/2
local reg = Region3.new(pos - size, pos + size)
local ignore = {workspace.Baseplate,d,ignorep}
local n = workspace:FindPartsInRegion3WithIgnoreList(reg,ignore,100)
if #n <= 0 then
print("no parts")
mouse.TargetFilter = nil
_G.down = false
_G.target = nil

else
print("parts")
		
end
end

1 comment

0

Before line 5, check if 'd' is nil.

1 answer

When the player's mouse is over no part, it will return as nil. How you fix this is by checking whether the target is nil or not, and if it isn't you carry on.

Here is your code:

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Button1Up:connect(function()
	while wait() do
		local ignorep = mouse.Target.Parent
		if ignorep ~= nil then --Checks to see if it is nil or not
			local d = mouse.TargetFilter
			local pos = d.Position
			local size = d.Size/2
			local reg = Region3.new(pos - size, pos + size)
			local ignore = {workspace.Baseplate,d,ignorep}
			local n = workspace:FindPartsInRegion3WithIgnoreList(reg,ignore,100)
			if #n <= 0 then
				print("no parts")
				mouse.TargetFilter = nil
				_G.down = false
				_G.target = nil
			else
				print("parts")
			end
		end
	end
end)

2 comments

0

It still do the same thing

0

check if d is nil or not then

This discussion is preserved as it was. New answers, comments, and votes are closed.