克隆+拖动模型时,按钮是点击ROBLOX [LUA]



所以我想有这么长时间,我只是不知道从哪里开始。我对这门语言很陌生,我一直在学习,但对我来说有点难。但我已经建立了自己的自定义角色,这花了我2周的时间。不管怎样,对于我的问题。举个例子,如果我有一个按钮,点击它,一个模型就会克隆出来我可以拖拽那个模型,把它放在附近的任何地方。我可以用什么方法来实现这个目标?

首先,我建议您以后有任何问题,请访问https://scriptinghelpers.org/

现在,关于你的问题,要克隆模型,你应该使用mouse.Target. parent:Clone()或我的函数库中的GetTopParent(mouse.Target)函数(你可以在这里得到;http://www.roblox.com/item.aspx?id=244244638)

然后将模型存入工作区,并使用makejoint ()

下一步是移动模型,这可能比较棘手,但最简单的方法是model:MoveTo(mouse. hit .p) on mouse。移动(但这有一点bug)

移动的另一种方法是使用Handles类,但我不是很熟悉它,所以你必须自己找出一个。

为了减少第一个方法的bug,我建议使用

model:MoveTo(mouse.Hit.p.X, mouse.Target.Position.Y + (model:GetExtentsSize().Y / 2), mouse.Hit.p.Z) 

但是你必须设置鼠标来忽略模型,这是我无法帮助的。

一个很好的开始是在Studio工具箱中搜索免费模型的'Dragger Tool'或'Model Dragger Tool',然后使用里面的脚本开始创建自己的模型。我已经学会了通过这样做来创建我自己的自定义滚动块,这比你一开始想象的要容易得多。一旦你找到了一个好的dragger工具来借用代码,如果你需要增强它,你可以在Roblox Wiki中找到dragger api来帮助你进一步定制它以满足你的特定需求。

http://wiki.roblox.com/index.php?title=API类/滚动块

编辑:所以这是第一个dragger脚本,显示当我搜索。它将拖动模型和部件,但您必须使用dragger api对其进行编辑以满足您的要求。在播放器中创建一个工具。然后在工具中创建一个LocalScript,然后复制并粘贴下面的代码到LocalScript中,这将让你开始。

        local Tool = script.Parent
    enabled = true
    local origTexture =     Tool.TextureId
    game:GetService("ContentProvider"):Preload("rbxasset://icons/freemove_sel.png")
    local selectionBox
    local currentSelection
    local currentSelectionColors = {}
    local selectionLasso
    local inGui = false
    local inPalette = false
    local lockTime = 0
    function canSelectObject(part)
        return part and not (part.Locked) and (part.Position - script.Parent.Parent.Head.Position).Magnitude < 60
    end
    function findModel(part)
        while part ~= nil do
            if part.className == "Model" then
                return part
            end
            part = part.Parent
        end
        return nil
    end

    function startDrag(mousePart, hitPoint, collection)
        dragger = Instance.new("Dragger")
        pcall(function() dragger:MouseDown(mousePart, hitPoint, collection) end)
    end
    function collectBaseParts(object, collection)
        if object:IsA("BasePart") then
            collection[#collection+1] = object
        end
        for index,child in pairs(object:GetChildren()) do
            collectBaseParts(child, collection)
        end
    end
    function onMouseDown(mouse) 
        mouse.Icon ="rbxasset://textures\GrabRotateCursor.png"
        local part = mouse.Target
        if canSelectObject(part) then
            local hitPoint = mouse.Hit:toObjectSpace(part.CFrame).p
            if trySelection(part) then
                local instances = {}
                collectBaseParts(currentSelection, instances)
                startDrag(part, hitPoint, instances)
                return
            end
        end
        --Clear the selection if we weren't able to lock succesfullu
        onMouseUp(mouse)
    end

    function onMouseUp(mouse)
        mouse.Icon ="rbxasset://textures\GrabCursor.png"
        if dragger ~= nil then
            pcall(function() dragger:MouseUp() end)
            dragger = nil
        end
    end
    function trySelection(part)
        if canSelectObject(part) then
            selectionLasso.Part = part
            local model = findModel(part)
            if model then       
                return setSelection(model)
            else
                return setSelection(part)
            end
        else
            clearSelection()
            return false
        end
    end
    function onKeyDown(key)
        if dragger ~= nil then
            if key == 'R' or key == 'r'  then
                dragger:AxisRotate(Enum.Axis.Y)
            elseif key == 'T' or key == 't' then
                dragger:AxisRotate(Enum.Axis.Z)
            end
        end
    end
    local alreadyMoving
    function onMouseMove(mouse)
        if alreadyMoving then
            return
        end
        alreadyMoving = true
        if dragger ~= nil then
            --Maintain the lock
            if time() - lockTime > 3 then
                Instance.Lock(currentSelection)
                lockTime = time()
            end
            --Then drag
            pcall(function() dragger:MouseMove(mouse.UnitRay) end)
        else
            trySelection(mouse.Target)
        end
        alreadyMoving = false
    end

    function saveSelectionColor(instance)
        if instance:IsA("BasePart") then
            currentSelectionColors[instance] = instance.BrickColor
            if instance.BrickColor == BrickColor.Blue() then
                instance.BrickColor = BrickColor.new("Deep blue")
            else
                instance.BrickColor = BrickColor.Blue()
            end
        end
        local children = instance:GetChildren() 
        if children then
            for pos, child in pairs(children) do
                saveSelectionColor(child)
            end
        end
    end
    function setSelection(partOrModel)
        if partOrModel ~= currentSelection then
            clearSelection()
            if Instance.Lock(partOrModel) then
                lockTime = time()
                currentSelection = partOrModel
                saveSelectionColor(currentSelection)
                selectionBox.Adornee = currentSelection
                return true
            end
        else
            if currentSelection ~= nil then
                if time() - lockTime > 2 then
                    --Maintain the lock
                    if not(Instance.Lock(currentSelection)) then
                        --we lost the lock
                        clearSelection()
                        return false
                    else
                        lockTime = time()
                        return true
                    end
                else
                    return true
                end
            end
        end
        return false
    end
    function clearSelection()
        if currentSelection ~= nil then
            for part, color in pairs(currentSelectionColors) do
                part.BrickColor = color
            end
            selectionBox.Adornee = nil
            Instance.Unlock(currentSelection)
        end
        currentSelectionColors = {}
        currentSelection = nil
        selectionLasso.Part = nil
        selectionBox.Adornee = nil
    end
    function onEquippedLocal(mouse)
        Tool.TextureId = "rbxasset://icons/freemove_sel.png"
        local character = script.Parent.Parent
        local player = game.Players:GetPlayerFromCharacter(character)

        inGui = false
        inPalette = false
        mouse.Icon ="rbxasset://textures\GrabCursor.png"
        mouse.Button1Down:connect(function() onMouseDown(mouse) end)
        mouse.Button1Up:connect(function() onMouseUp(mouse) end)
        mouse.Move:connect(function() onMouseMove(mouse) end)
        mouse.KeyDown:connect(function(string) onKeyDown(string) end)
        selectionBox = Instance.new("SelectionBox")
        selectionBox.Name = "Model Delete Selection"
        selectionBox.Color = BrickColor.Blue()
        selectionBox.Adornee = nil
        selectionBox.Parent = player.PlayerGui
        selectionLasso = Instance.new("SelectionPartLasso")
        selectionLasso.Name = "Model Drag Lasso"
        selectionLasso.Humanoid = character.Humanoid
        selectionLasso.archivable = false
        selectionLasso.Visible = true
        selectionLasso.Parent = game.workspace
        selectionLasso.Color = BrickColor.Blue()
        alreadyMoving = false
    end
    function onUnequippedLocal()
        Tool.TextureId = origTexture
        clearSelection()
        selectionBox:Remove()
        selectionLasso:Remove()
    end

    Tool.Equipped:connect(onEquippedLocal)
    Tool.Unequipped:connect(onUnequippedLocal)

最新更新