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

Mouse wont detect object anymore until I pick up another?

Title is confusing yea. What I mean is, When I pick up an object and then drop it, the mouse wont detect the same object anymore. But when I pick up another object and drop it, the mouse is able to detect the previous object now, but not the current one.

Here's an illustration of what I'm talking about. https://gyazo.com/308007f5ee8e64bcb3346d698e1a0d87

Script:

--Variables
local RunService = game:GetService("RunService")
local InputService = game:GetService("UserInputService")
local CP = game:GetService("ContentProvider")
local Debris = game:GetService("Debris")

local Player = game.Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local Mouse = Player:GetMouse()
repeat wait() until Player.Character
local Character = Player.Character
local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")
local Torso = Character:WaitForChild("Torso")

local DragTarget
local DragMinDistance = 8
local DragTargetDistance = 4

local Crosshair = 754262000
local SelectionCrosshair = 754262000

local mousehoverdist = 15

--DragObject
InputService.InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		if not DragTarget and Mouse.Target ~= nil then
			local t = Mouse.Target
			print(t.Name)
			if t and t.Parent and not t.Parent:FindFirstChild("Humanoid") and t.Anchored == false and t.Transparency < 1 and t.Size.magnitude <= 40 and (Head.Position-t.Position).magnitude <= DragMinDistance+t.Size.magnitude/2 and t:FindFirstChild("Object") then
				if t:FindFirstChild("Object").Value == true then
					DragTarget = t
					local bp = Instance.new("BodyPosition", DragTarget)bp.Name = "DragPosition"
					bp.MaxForce = Vector3.new(6000,6000,6000)
					bp.P = 5500
					bp.D = 400
					Mouse.TargetFilter = DragTarget
					DragTargetDistance = (Head.CFrame.p-DragTarget.Position).magnitude
					bp.Position = (Head.Position + (Head.CFrame.p - Mouse.Hit.p).unit*-8)
					local bg = Instance.new("BodyGyro", DragTarget)bg.CFrame = DragTarget.CFrame
					bg.MaxTorque = Vector3.new(0,0,0)
					bg.Name = "DragGyro"
					coroutine.wrap(function()
						while true do wait()
							if DragTarget ~= nil then
								dragMove()
							else break
							end
						end
					end)()
				end
			end
		elseif DragTarget ~= nil then
			DragTarget.Velocity = Vector3.new(0,0,0)
			DragTarget.DragPosition:Destroy()
			DragTarget.DragGyro:Destroy()
			DragTarget = nil
		end
	end
end)

function dragMove()
	if DragTarget then
		DragTargetDistance = (Head.CFrame.p-DragTarget.Position).magnitude/2
		DragTarget.DragPosition.Position = (Head.Position + (Head.CFrame.p - Mouse.Hit.p).unit*-8)
	end
	if Mouse.Target ~= nil then
		if Mouse.Target:FindFirstChild("ClickDetector") and (Mouse.Target.CFrame.p - Torso.CFrame.p).magnitude <= mousehoverdist then
			Mouse.Icon = "rbxassetid://"..tostring(SelectionCrosshair)
		else
			Mouse.Icon = "rbxassetid://"..tostring(Crosshair)
		end
	end
end
Mouse.Move:connect(dragMove)

I'm confused, I'm just thinking it's a bug, or some local error.

1 answer

✓ Accepted answer

The problem here lies in line 38

Mouse.TargetFilter = DragTarget

What happens here is you set the Mouse.TargetFilter to the DragTarget but you never remove it. This means that until you set a new TargetFilter, the old one stays on the old DragTarget. To fix this you can simply set Mouse.TargetFilter = nil where you set DragTarget = nil like this:

	elseif DragTarget ~= nil then
		DragTarget.Velocity = Vector3.new(0,0,0)
		DragTarget.DragPosition:Destroy()
		DragTarget.DragGyro:Destroy()
		Mouse.TargetFilter = nil
		DragTarget = nil
	end

Hope this helped and if it did be sure to mark this answer as answered. Have a nice day!

1 comment

0

Yup! It worked! That totally makes sense, cant believe I missed that, thank you!

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