Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

Detecting if there's something on mouse position?

Asked by
npott13 23
7 years ago
Edited 7 years ago

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

0
Before line 5, check if 'd' is nil. Goulstem 8144 — 7y

1 answer

Log in to vote
0
Answered by 7 years ago
Edited 7 years ago

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)
0
It still do the same thing npott13 23 — 7y
0
check if d is nil or not then User#9949 0 — 7y
Ad

Answer this question