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

Collision Detection issues?

Asked by
Bman8765 270 Moderation Voter
9 years ago

I have a custom Inventory GUI I'm making but I'm running across some issues during the development. Here is my current problem though; I want user to be able to drag and drop items to new slots in their inventory and organize it to their own style. I decided to use roblox's open source collision detection for GUI's but it does not appear to be working.

Info: all these things are located in multiple Frames in a ScreenGUI. FILTERING ENABLED is true (I don't think this will effect your answer but please keep this in mind).

If you are still confused or don't understand my question just message my roblox account or leave a comment. My apologies if this does not make any sense, I'm not the best at explaining.

I am currently messing with this but I wanted to hear what others had to say, thanks!

The code (in a localscript obv)

repeat wait() until game.Players.LocalPlayer
--------------------------------------------------------
--Variables
--------------------------------------------------------
local StarterGui = game:GetService('StarterGui')
local Player = game.Players.LocalPlayer
local inventory = script.Parent.Inventory
local hotbar = script.Parent.HotBar
--------------------------------------------------------
-- Startup process
-------------------------------------------------------- 
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Health, false)
StarterGui:SetCoreGuiEnabled(Enum.CoreGuiType.Backpack, false)

inventory.Visible = false
hotbar.Visible = true
-------------------------------------------------------
--Bman's Sexy Inventory API
-------------------------------------------------------
function sortInv()
    print("sorting")

    for _,item in pairs (script.Parent.items:GetChildren()) do
        for _,slot in pairs (script.Parent.Inventory:GetChildren()) do
            print(slot.Name)
            if (slot.Name ~= "INFO") and (slot.ClassName == "Frame") and (slot.containsItem.Value == false) then
                print(slot.Name)                
                slot.containsItem.Value = true
                local cloneItem = item:Clone()
                cloneItem.Parent = slot
                break
            end
        end
    end
end

function returnItemsInInv()
    local items = {}
    for _,slot in pairs (script.Parent.Inventory:GetChildren()) do
        if (#slot:getChildren() > 1) then
            for _,item in pairs (slot:getChildren()) do
                if (item.ClassName == "ImageLabel") then
                    local num = table.maxn(items)
                    table.insert(items, num, item)
                end
            end
        end
    end
    return items
end

function checkCollision(gui)
    local items = returnItemsInInv()
    for _, gui2 in pairs (script.Parent.Inventory:GetChildren()) do
        print(gui2.Name)
        if (gui2.Name == "INFO") then
            break
        end
        if gui.Position.Y.Scale<gui2.Position.Y.Scale+gui2.Size.Y.Scale and
            gui.Position.Y.Scale>gui2.Position.Y.Scale-(gui2.Size.Y.Scale/2) and
            gui.Position.X.Scale<gui2.Position.X.Scale+gui2.Size.X.Scale and
            gui.Position.X.Scale+gui.Size.X.Scale>gui2.Position.X.Scale then
            --Downward edge collide
            gui.Position=UDim2.new(gui2.Position)
            gui.Parent = gui2
            print("boom 1")
            return
        end
        if gui.Position.Y.Scale+gui.Size.Y.Scale>gui2.Position.Y.Scale and
            gui.Position.Y.Scale+gui.Size.Y.Scale<gui2.Position.Y.Scale+(gui2.Size.Y.Scale/2) and
            gui.Position.X.Scale<gui2.Position.X.Scale+gui2.Size.X.Scale and
            gui.Position.X.Scale+gui.Size.X.Scale>gui2.Position.X.Scale then
            -- Upward edge collide
            gui.Position=UDim2.new(gui2.Position)
            gui.Parent = gui2.Parent
            print("boom 2")
            return
        end
        if gui.Position.X.Scale+gui.Size.X.Scale>gui2.Position.X.Scale and
            gui.Position.X.Scale+gui.Size.X.Scale<gui2.Position.X.Scale-(gui2.Size.X.Scale/2) and
            gui.Position.Y.Scale<gui2.Position.Y.Scale+gui2.Size.Y.Scale and
            gui.Position.Y.Scale+gui.Size.Y.Scale>gui2.Position.Y.Scale then
            -- Left edge collide
            gui.Position=UDim2.new(gui2.Position)
            gui.Parent = gui2.Parent
            print("boom 3")
            return
        end
        if gui.Position.X.Scale<gui2.Position.X.Scale+gui2.Size.X.Scale and
            gui.Position.X.Scale>gui2.Position.X.Scale+(gui2.Size.X.Scale/2) and
            gui.Position.Y.Scale<gui2.Position.Y.Scale+gui2.Size.Y.Scale and
            gui.Position.Y.Scale+gui.Size.Y.Scale>gui2.Position.Y.Scale then
            -- Right edge collide
            gui.Position=UDim2.new(gui2.Position)
            gui.Parent = gui2.Parent
            print("boom 4")
            return
        end
    end

end

sortInv() --TEMPORARY
-------------------------------------------------------
--Keyboard Input
-------------------------------------------------------
local Mouse = Player:GetMouse()

Mouse.KeyDown:connect(function(Key)
    print(("The '%s' key was pressed"):format(Key))
    if (Key == "e") then
        if (inventory.Visible == false) then
            inventory.Visible = true
        elseif (inventory.Visible == true) then
            inventory.Visible = false
        end
    end
end)
-------------------------------------------------------
--Dragging items
-------------------------------------------------------
while true do
    local items = returnItemsInInv()
    for _,item in pairs (items) do
        item.DragStopped:connect(function(X, Y)
            print(("The player stopped dragging the GuiObject at (%i, %i)"):format(X, Y))
            checkCollision(item)
        end)
    end
    wait()
end

Answer this question