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

Why Dragger is not moving the model?

Asked by 7 years ago

I would like a Dragger to move a model, but It doesn't work.

Here is my script:

local Dragger = Instance.new("Dragger")
local MouseDown = false
local plr = game.Players.LocalPlayer

script.Parent.Equipped:connect(function()
    local Mouse = plr:GetMouse()
    if not Mouse then return end

    Mouse.Button1Down:connect(function()
        print("Down")
        Instance.new("SelectionBox",game.Players.LocalPlayer.PlayerGui).Adornee = Mouse.Target
        print("Selected")
        if Mouse.Target.Locked == true then MouseDown = false print("Locked") return end
        print("Brick")
        if Mouse.Target.Parent:IsA("Model") then
            MouseDown = true
            local list = Mouse.Target.Parent:GetChildren()
            Dragger:MouseDown(Mouse.Target,Mouse.Target.Position - Mouse.Hit.p,list)
        else
            MouseDown = true
            Dragger:MouseDown(Mouse.Target,Mouse.Target.Position - Mouse.Hit.p,{Mouse.Target})
        end
    end)

    Mouse.Move:connect(function()
        if MouseDown == true then
            Dragger:MouseMove(Mouse.UnitRay)
        end
    end)

    Mouse.Button1Up:connect(function()
        if MouseDown == true then
            MouseDown = false
            for _, selec in pairs(plr.PlayerGui:GetChildren()) do
                if selec:IsA("SelectionBox") then
                    selec:Destroy()
                end
            end
            Dragger:MouseUp()
        end
    end)

    Mouse.KeyDown:connect(function(Key)
        if MouseDown == true then
            if Key == "r" then
                Dragger:AxisRotate("Y")
            elseif Key == "t" then
                Dragger:AxisRotate("X")
            end
        end
    end)

end)

I made a list with :GetChildren() but it doesn't work and it says Mouse part should be in list I tried to write this line:

Dragger:MouseDown(Mouse.Target.Parent,Mouse.Target.Position - Mouse.Hit.p,{Mouse.Target.Parent})

Not working too.

Thanks if you could help me.

1 answer

Log in to vote
1
Answered by 7 years ago
Edited 7 years ago

So you almost had it except you did make a few errors, and used a some methods that are deprecated. Instead of using Mouse.KeyDown we will be using UserInputService because Mouse.KeyDown is deprecated. So for example

local UserInput = game:GetService('UserInputService')
UserInput.InputBegan:connect(function(Input)
    if Input.UserInputType == Enum.UserInputType.KeyBoard then --Check if the input was from the keyboard
        if Input.KeyCode == Enum.KeyCode.R then --Checks if the KeyCode of the Key pressed matches the Enum KeyCode for R
            print("R was pressed")
        end
    end
end)

Also you didn't account that when you unequip the tool the script will still run. So we will use a few connections to fix this problem. So for example

local Con = nil
script.Parent.Equipped:connect(function()
Con = game.Players.LocalPlayer.Mouse.Button1Down:connect(function()
end)
script.Parent.UnEquipped:connect(function()
Con:disconnect()
end)

That is a example of using connections so that when the tool is unequipped the function is no longer connected, in short the code won't run. You also didn't account parts that aren't models and are parented to workspace. So to fix this I added a few lines

if Mouse.Target.Parent == game.Workspace then
    MouseDown = true
        Dragger:MouseDown(Mouse.Target,Mouse.Target.Position - Mouse.Hit.p,{Mouse.Target})

So all together now

local Plr = game.Players.LocalPlayer
local Dragger = Instance.new('Dragger')
local MouseDown = nil
local MB1DCon = nil
local MB1UCon = nil
local MMCon = nil
local UserInput = game:GetService('UserInputService')
local UISCon = nil

script.Parent.Equipped:connect(function()
    local Mouse = Plr:GetMouse()
    if not Mouse then return end

    MB1DCon = Mouse.Button1Down:connect(function()
        Instance.new("SelectionBox",game.Players.LocalPlayer.PlayerGui).Adornee = Mouse.Target
        if Mouse.Target.Locked == true then MouseDown = false print("Locked") return end
        if Mouse.Target.Parent == game.Workspace then
            MouseDown = true
            Dragger:MouseDown(Mouse.Target,Mouse.Target.Position - Mouse.Hit.p,{Mouse.Target})
        elseif Mouse.Target.Parent:IsA("Model")then
            MouseDown = true
            local list = Mouse.Target.Parent:GetChildren()
            for i=1,#list do
                if not list[i]:IsA('BasePart') then
                    table.remove(list,i)
                end
            end
            Dragger:MouseDown(Mouse.Target,Mouse.Target.Position - Mouse.Hit.p,list)
        else
            MouseDown = true
            Dragger:MouseDown(Mouse.Target,Mouse.Target.Position - Mouse.Hit.p,{Mouse.Target})
        end
    end)

    MB1UCon = Mouse.Button1Up:connect(function()
        if MouseDown then
            MouseDown = false
            for _, selec in pairs(Plr.PlayerGui:GetChildren()) do
                if selec:IsA("SelectionBox") then
                    selec:Destroy()
                end
            end
            Dragger:MouseUp()
        end
    end)

    MMCon = Mouse.Move:connect(function()
        if MouseDown then
            Dragger:MouseMove(Mouse.UnitRay)
        end
    end)

    UISCon = UserInput.InputBegan:connect(function(Input)
        if Input.UserInputType == Enum.UserInputType.Keyboard then
            if Input.KeyCode == Enum.KeyCode.R then
                if MouseDown then
                    Dragger:AxisRotate("Y")
                end
            elseif Input.KeyCode == Enum.KeyCode.Q then
                if MouseDown then
                    Dragger:AxisRotate("X")
                end 
            end
        end
    end)
end)

script.Parent.Unequipped:connect(function()
    UISCon:disconnect()
    MB1DCon:disconnect()
    MB1UCon:disconnect()
    MMCon:disconnect()
end)

Hope you don't just use this script but learn from it.

Ad

Answer this question