#UIDragDetectors

1 messages · Page 1 of 1 (latest)

celest epoch
#

Im trying to create a drag and drop system however ive come across a problem, for some reason the mouseisin variable is always the same button. I know what the problem is i just have no idea how i can fix it


function Sorting.Drag()
    for _, v in Sorting.uiElements do
        local Imagelabel = v.ImageLabel
        local Drag = Imagelabel:FindFirstChildOfClass("UIDragDetector")
        if not Drag then print("Drag not found".. v.Name) continue end
        
        local Bool = v:FindFirstChildOfClass("BoolValue")
        if not Bool then print("Bool not found") continue end
        
        local MouseIsIn
        local StartPosition
        Drag.DragStart:Connect(function()
            print("Drag started")
            StartPosition = Imagelabel.Position
        end)
        Drag.DragEnd:Connect(function()
            print("Drag Ended")
            if MouseIsIn ~= nil then
                if MouseIsIn == v then return end
                local NewBool = MouseIsIn:FindFirstChildOfClass("BoolValue")
                local Tool = Bool:FindFirstChildOfClass("Tool")
                if not Tool then print("Tool not found") return end
                
                Tool.Parent = NewBool
                print(Tool.Parent.Parent.Name)
                --MouseIsIn.ImageLabel.Visible = true
                --Imagelabel.Image = 0
                print("Changed Image")
                --MouseIsIn = nil
            end
            Imagelabel.Position = StartPosition
        end)
        
        v.MouseEnter:Connect(function()
            MouseIsIn = v
            if Bool.Value then
                print("item "..v.Name)
                return 
            end
            print("no item "..v.Name)
        end)
        
    end
end
celest epoch
#

The problem is fixed, my only question is how i can shorten this furhter? ```lua
local function ChangeImagePos(Imagelabel, StartPosition)
Imagelabel.Position = StartPosition
end

function Sorting.Drag()
for _, v in Sorting.uiElements do
local Imagelabel = v.ImageLabel
local Drag = Imagelabel:FindFirstChildOfClass("UIDragDetector")
if not Drag then print("Drag not found".. v.Name) continue end

    local Bool = v:FindFirstChildOfClass("BoolValue")
    if not Bool then print("Bool not found") continue end
    
    local MouseIsIn
    local StartPosition
    Drag.DragStart:Connect(function()
        print("Drag started")
        StartPosition = Imagelabel.Position
    end)
    Drag.DragEnd:Connect(function()
        print("Drag Ended")
        if MouseIsIn ~= nil then
            if MouseIsIn == v then return end
            local NewBool = MouseIsIn:FindFirstChildOfClass("BoolValue")
            local Tool = Bool:FindFirstChildOfClass("Tool")
            if not Tool then print("Tool not found") ChangeImagePos(Imagelabel, StartPosition) return end
            
            Tool.Parent = NewBool
            NewBool.Value = true
            Imagelabel.Visible = false
            Bool.Value = false
            print(Tool.Parent.Parent.Name)
            print("Changed Image")
        end
        ChangeImagePos(Imagelabel, StartPosition)
    end)
    
    for _, v2 in Sorting.uiElements do
        v2.MouseEnter:Connect(function()
            if v2 == v then return end
            MouseIsIn = v2
            if Bool.Value then
                print("item "..v.Name)
                return 
            end
            print("no item "..v.Name)
        end)
        v2.MouseLeave:Connect(function()
            MouseIsIn = nil
        end)
    end

end

end

nova orchid
#

are you trying to make it look cleaner

#

or shorten the script

celest epoch
#

both