Почему событие MouseClick дублируется? - PullRequest
0 голосов
/ 01 октября 2019

Событие щелчка мышью в локальном скрипте дублируется. Каждый раз, когда щелкает мышь, она регистрируется дважды.

Я использую встроенную Мышь , которую я получил от game.Players.LocalPlayer:GetMouse() для Mouse.Target, но я также использовал Библиотека RbxMouse .

У меня есть скрипт экрана загрузки, который отключает все элементы управления с помощью ContextActionService, и скрипт для размещения блоков со стороны клиента. Да, я знаю, что это грязно, и блоки размещаются только на стороне клиента, а не на сервере, но это WIP

-- The Block Placement Script

local player = game.Players.LocalPlayer
local char = player.Character
local mouse = require(script.RbxMouse)
local bM = player:GetMouse()
local UserInputService = game:GetService("UserInputService")
local grass = script:WaitForChild("Grass")
local pe = script:WaitForChild("PE")

local tweenService = game:GetService("TweenService")

local reachDisplayMod = script.Parent:WaitForChild("ReachDisplayMod")
local reach = reachDisplayMod.Reach

local defaultHardness = require(script:WaitForChild("DefaultHardness"))
local hardnessGui = game.Lighting:WaitForChild("HardnessGUI")

local count = 10000
local reachWaitTime = 0

local hardnessWaitTime = 0

local sb = script:WaitForChild("SelectionBox")

local isMining = false
local was = nil

function round(num, numDecimalPlaces)
  local mult = 10^(numDecimalPlaces or 0)
  return math.floor(num * mult + 0.5) / mult
end

function placeBlock(block, vector, ts)
    local bl = block:Clone()
    print("Placing block!")
    if(ts == "Top") then
        bl.Hitbox.Position = vector + Vector3.new(0, 4, 0)
        if(bl.Hitbox:FindFirstChild("Garnish")) then
            bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
        end
        bl.Parent = workspace.Blocks
    elseif(ts == "Front") then
        bl.Hitbox.Position = vector - Vector3.new(0, 0, 4)
        if(bl.Hitbox:FindFirstChild("Garnish")) then
            bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
        end
        bl.Parent = workspace.Blocks
    elseif(ts == "Right") then
        bl.Hitbox.Position = vector + Vector3.new(4, 0, 0)
        if(bl.Hitbox:FindFirstChild("Garnish")) then
            bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
        end
        bl.Parent = workspace.Blocks
    elseif(ts == "Left") then
        bl.Hitbox.Position = vector - Vector3.new(4, 0, 0)
        if(bl.Hitbox:FindFirstChild("Garnish")) then
            bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
        end
        bl.Parent = workspace.Blocks
    elseif(ts == "Back") then
        bl.Hitbox.Position = vector + Vector3.new(0, 0, 4)
        if(bl.Hitbox:FindFirstChild("Garnish")) then
            bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
        end
        bl.Parent = workspace.Blocks
    elseif(ts == "Bottom") then
        bl.Hitbox.Position = vector - Vector3.new(0, 4, 0)
        if(bl.Hitbox:FindFirstChild("Garnish")) then
            bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
        end
        bl.Parent = workspace.Blocks
    end
    if(was ~= nil) then
        was:Destroy()
        was = nil
    end
    local x = sb:Clone()
    x.Parent = bl
    x.Adornee = bl
    was = x
end

function setReach(num)
    num = round(num, 2)
    reach.Text = tostring(num)
    reachWaitTime = reachWaitTime + 2
    coroutine.wrap(function()
        wait(reachWaitTime)
        reach.Text = "0"
    end)()
end

local function calcDec(min, max)
    local val = (min / 100) / (max / 100)
    return val
end

local function tween(inst, t, goal)
    local info = TweenInfo.new(t)
    local tw = tweenService:Create(inst, info, goal)
    tw:Play()
end

local function setProgress(inst, min, max)
    hardnessWaitTime = hardnessWaitTime + 3
    if(inst:FindFirstChild("HardnessGUI")) then
        local g = inst.HardnessGUI
        tween(g.BarBG.Bar, .2, {
            Size = UDim2.new(calcDec(min, max), 0, 1, 0)
        })
        coroutine.wrap(function()
            wait(hardnessWaitTime)
            g:Destroy()
        end)()
    else
        local g = hardnessGui:Clone()
        g.Parent = inst
        tween(g.BarBG.Bar, .2, {
            Size = UDim2.new(calcDec(min, max), 0, 1, 0)
        })
        coroutine.wrap(function()
            wait(hardnessWaitTime)
            g:Destroy()
        end)()
    end
end

function setReach(num)
    num = round(num, 2)
    reach.Text = tostring(num)
    reachWaitTime = reachWaitTime + 2
    coroutine.wrap(function()
        wait(reachWaitTime)
        reach.Text = "0"
    end)()
end

mouse.Button[1].Down:Connect(function()
    local t = bM.Target
    if(t.Name == "Hitbox") then
        local head = char:FindFirstChild("Head")
        local pos = (head.Position - t.Position).Magnitude / 4

        if(pos < 3 or pos == 3) then
            isMining = true
            setReach(pos)
            local block = t.Parent
            local conf = require(block:WaitForChild("BlockConfig"))
            local bName = conf.Name
            local hardness = block.Hardness
            print(hardness.Value)
            hardness.Value = hardness.Value - 1

            setProgress(block, hardness.Value, defaultHardness[bName] or 5)

            if(hardness.Value <= 0) then
                block:Destroy()
            end
            isMining = false

            local x = pe:Clone()
            x.Texture = "http://www.roblox.com/asset/?id=63244754"
            x.Parent = t.Parent
            wait(3)
            x:Destroy()
        end
    elseif(t.Parent.Name == "Hitbox") then
        local head = char:FindFirstChild("Head")
        local pos = (head.Position - t.Position).Magnitude / 4

        if(pos < 3 or pos == 3) then
            isMining = true
            setReach(pos)
            local block = t.Parent.Parent
            local conf = require(block:WaitForChild("BlockConfig"))
            local bName = conf.Name
            local hardness = block.Hardness
            print(hardness.Value)
            hardness.Value = hardness.Value - 1

            setProgress(block, hardness.Value, defaultHardness[bName] or 5)

            if(hardness.Value <= 0) then
                block:Destroy()
            end
            isMining = false

            local x = pe:Clone()
            x.Texture = "http://www.roblox.com/asset/?id=63244754"
            x.Parent = t.Parent
            wait(3)
            x:Destroy()

        end
    end
end)

mouse.Button[2].Down:Connect(function()
    local target = mouse.Target
    if(count >= 1 and (mouse.Target.Name == "Hitbox" or mouse.Target.Name == "Garnish")) then
        local bl = grass:Clone()
        local mp = mouse.CFrame.p
        local newPos = Vector3.new(round(mp.X, 4), round(mp.Y, 4), round(mp.Z, 4))
        bl.Name = "Block"

        local ts = bM.TargetSurface.Name
        if(mouse.Target.Name == "Garnish") then target = mouse.Target.Parent end

        placeBlock(bl, target.Position, ts)
        count = count - 1
        --[[
        if(mouse.Target.Name == "Garnish") then target = mouse.Target.Parent end
        if(ts == "Top") then
            bl.Hitbox.Position = target.Position + Vector3.new(0, 4, 0)
            if(bl.Hitbox:FindFirstChild("Garnish")) then
                bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
            end
            bl.Parent = workspace.Blocks
        elseif(ts == "Front") then
            bl.Hitbox.Position = target.Position - Vector3.new(0, 0, 4)
            if(bl.Hitbox:FindFirstChild("Garnish")) then
                bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
            end
            bl.Parent = workspace.Blocks
        elseif(ts == "Right") then
            bl.Hitbox.Position = target.Position + Vector3.new(4, 0, 0)
            if(bl.Hitbox:FindFirstChild("Garnish")) then
                bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
            end
            bl.Parent = workspace.Blocks
        elseif(ts == "Left") then
            bl.Hitbox.Position = target.Position - Vector3.new(4, 0, 0)
            if(bl.Hitbox:FindFirstChild("Garnish")) then
                bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
            end
            bl.Parent = workspace.Blocks
        elseif(ts == "Back") then
            bl.Hitbox.Position = target.Position + Vector3.new(0, 0, 4)
            if(bl.Hitbox:FindFirstChild("Garnish")) then
                bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
            end
            bl.Parent = workspace.Blocks
        elseif(ts == "Bottom") then
            bl.Hitbox.Position = target.Position - Vector3.new(0, 4, 0)
            if(bl.Hitbox:FindFirstChild("Garnish")) then
                bl.Hitbox.Garnish.Position = bl.Hitbox.Position + Vector3.new(0, 1.53, 0)
            end
            bl.Parent = workspace.Blocks
        end
        count = count - 1

        --tween(bl.Hitbox, 2, {Transparency = 0; CanCollide = true})
        ]]
    end
end)

game:GetService("RunService").RenderStepped:Connect(function()
    local t = bM.Target
    if(t ~= nil) then
        if(t.Name == "Hitbox") then
            if(was ~= nil) then
                was:Destroy()
                was = nil
            end
            local x = sb:Clone()
            x.Parent = t.Parent
            x.Adornee = t.Parent
            was = x
        elseif(t.Parent.Name == "Hitbox") then
            if(was ~= nil) then
                was:Destroy()
                was = nil
            end
            local x = sb:Clone()
            x.Parent = t.Parent.Parent
            x.Adornee = t.Parent.Parent
            was = x
        else
            if(was ~= nil) then
                was:Destroy()
                was = nil
            end
        end
    end
end)

game.Players.LocalPlayer.CharacterRemoving:Connect(function()
    if(was ~= nil) then
        was:Destroy()
        was = nil
    end
end)
-- Loading Screen Script

local player = game.Players.LocalPlayer
local isLoading = true
local isWorldLoading = game.ReplicatedStorage:WaitForChild("IsLoadingWorld")
local runService = game:GetService("RunService")
local blockScript = script.Parent.Parent.BlockScript

local ContextActionService = game:GetService("ContextActionService")
local tweenService = game:GetService("TweenService")
local FREEZE_ACTION = "freezeMovement"
local FREEZE_CAMERA = "freezeCamera"

local bg = script.Parent:WaitForChild("Background")
local bar = bg:WaitForChild("Bar")
local info = bg:WaitForChild("Info")

local blur = Instance.new("BlurEffect")
blur.Parent = workspace.CurrentCamera
blur.Size = 10

-- Initialize Disabling Controls
blockScript.Disabled = true

-- Does this mess with the mouse and keyboard controls?
ContextActionService:BindAction(
    FREEZE_ACTION,
    function() return Enum.ContextActionResult.Sink end,
    false,
    unpack(Enum.PlayerActions:GetEnumItems())
)

ContextActionService:BindAction(
    FREEZE_CAMERA,
    function() return Enum.ContextActionResult.Sink end,
    false,
    Enum.UserInputType.MouseButton2
)

local function setInfo(text, color)
    color = color or Color3.new(255, 255, 255)

    info.Text = text
    info.TextColor3 = color
end

local function setProgress(min, max)
    local t = .5
    local i = TweenInfo.new(t)
    local tween = tweenService:Create(bar, i, {
        Size = UDim2.new((min / 100) / (max / 100), 0, 1, 0)
    })
    tween:Play()
end

setProgress(1, 2)
--[[
while(isWorldLoading) do
    setInfo("Generating terrain... (this could take a few minutes)")
    wait()
end
]]

setProgress(2, 2)
setInfo("You're good to go!")
wait(2)

local i = TweenInfo.new(1)
local t = tweenService:Create(blur, i, {
    Size = 0
})
t:Play()

local t = tweenService:Create(bg, i, {
    BackgroundTransparency = 1
})
t:Play()

local t = tweenService:Create(bar, i, {
    BackgroundTransparency = 1
})
t:Play()

local t = tweenService:Create(info, i, {
    TextTransparency = 1
})
t:Play()

wait(1.2)
script.Parent:Destroy()

-- Allow the player to move
blockScript.Disabled = false
ContextActionService:UnbindAction(FREEZE_CAMERA)
ContextActionService:UnbindAction(FREEZE_ACTION)

Так как он печатает каждый раз, когда щелкают мышью (твердость остается наблок) скажем, у нас есть блок с 5 твердостью, вместо того, чтобы убирать только 1 каждый клик, он убирает два. То же самое с размещением блока.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...