I have a function to check what the mouse is hovering over in the environment and to highlight certain selectable parts/models, which the player can then pickup and carry. When a player is then carrying one of these objects, the selection highlight is disconnected, but seems to remain active. Can somebody please explain where I am going wrong.
local mouseConnection
function enableSelectionBox()
mouseConnection = mouse.Move:Connect(function() --When the mouse moves
local target = mouse.Target
processTarget(target) -- Adds the highlight around the object
end)
end
function disableSelectionBox() -- The print shows, but the selection box can still highlight objects
print("DISABLE Selection Box")
mouseConnection:Disconnect()
mouseConnection = nil
end
-- Check when Player CARRYING Attribute changes
Player:GetAttributeChangedSignal("Carrying"):Connect(function()
if Player:GetAttribute("Carrying") == false then -- PLAYER CAN PICKUP
disableSelectionBox()
else
enableSelectionBox() -- PLAYER CANNOT PICKUP
end
end)
Any help on this would be greatly appreciated
Could you provide us the processTarget Function?
The procecssTarget function checks if the item is tagged via CollectionService:
local function processTarget(target)
if not target then
selectionBox.Adornee = nil
lastItem = nil
return
end
if (CollectionService:HasTag(target, "Pickup")) then
if lastItem ~= target then
selectionSnd:Play()
end
targetItem = target
if target:IsA("BasePart") then
selectionBox.Adornee = target
else
selectionBox.Adornee = nil
end
local partDist = Player:DistanceFromCharacter(target.Position)
if partDist > 10 + (5 * Player.Abilities.Pickup.Value) then
selectionBox.Color3 = Color3.new(0.784314, 0, 0) -- RED BOX
else
selectionBox.Color3 = Color3.new(0, 0.784314, 0) -- GREEN BOX
end
itemSelected = target
lastItem = targetItem
else
selectionBox.Adornee = nil
end
end
it is possible that the issue you’re facing is due to the processTarget() function, which adds the highlight around the object, continuing to run even after calling disableSelectionBox().
try this:
local mouseConnection
local function enableSelectionBox()
mouseConnection = mouse.Move:Connect(function()
processTarget(mouse.Target)
end)
end
local function disableSelectionBox()
print("disabled selection box")
mouseConnection:Disconnect()
clearTarget()
end
local function clearTarget()
-- code to remove the highlight from the object
end
Player:GetAttributeChangedSignal("Carrying"):Connect(function()
if not Player:GetAttribute("Carrying") then
disableSelectionBox()
else
enableSelectionBox()
end
end)