断开模块脚本与本地脚本的连接

Disconnecting a modulescript from local script

我正在制作一把不使用工具对象的枪engine/system。

目前我什么都没有了; 装备 射击 解除装备

枪支是内置模块脚本的模型。他们掌握着枪支的统计数据和相关功能(射击、焊接),但我遇到了一个问题

当玩家尝试装备一把已经装备好的枪支时,他会取消装备它(这应该发生),但他仍然可以发射取消装备的枪支。 shot/raycast来自枪被卸下前的最后位置。

如果我再次装备这把枪并开火,它会同时发射之前未装备的枪和装备好的枪。

我该如何解决这个问题?

equip 函数(在本地脚本中)

local function equip(gun)
    --[[
        Check to see if the currently being equipped gun is the same as the already equipped gun. If so, destroy equipped gun and return
    --]]
    if curEquipped~=nil and gun.Name == curEquipped.Model.Name then
        print("HI!")
        player.Character:FindFirstChild(gun.Name):Destroy()    
        curEquipped:Weld(player.Character.Torso,false)
        curEquipped = nil
        return
    end
    local gunMod = gun.Gun
    local gunModule = require(gunMod) -- not confusing at all.
    local newGun = gunModule.new()
    --[[
        Setting curEquipped
    --]]
    if not curEquipped then
        curEquipped = newGun
    else
        curEquipped:Weld(player.Character.Torso,false)
        curEquipped.Model:Destroy()
        curEquipped = newGun
    end
    gun.Parent = player.Character
    newGun:Weld(player.Character.Torso,true)
    mouse.Button1Down:connect(function()
        fire(newGun)
    end)
end

我正在测试的枪内模块脚本:

local gunStats = {}
gunStats.__index = gunStats

function gunStats.new()
    local newGun = {}
    setmetatable(newGun,gunStats)
    newGun.Welding = {}
    setmetatable(newGun.Welding,newGun)
    newGun.fireRate = 1
    newGun.Barrel = script.Parent.Barrel
    newGun.HandlePosition = script.Parent.HandlePos.Position
    newGun.MaxAccuracy = .6
    newGun.Accuracy = .2
    newGun.Recoil = 150
    newGun.Model = script.Parent
    newGun.Bullet = Instance.new("Part")
    newGun.Bullet.BrickColor = BrickColor.Yellow()
    newGun.Bullet.Size=Vector3.new(.2,.2,1)
    newGun.Bullet.Anchored = true
    newGun.Bullet.CanCollide = false
    newGun.mesh=Instance.new("SpecialMesh",newGun.Bullet)
    newGun.mesh.MeshType="Brick"
    newGun.mesh.Name = "Mesh"
    newGun.mesh.Scale = Vector3.new(.5,.5,1)
    newGun.IsWelded = false
    newGun.Welding.WeldLeftArm = CFrame.new(-0.35, 0.4, 0.8)*CFrame.fromEulerAnglesXYZ(math.rad(280), 0, math.rad(-90))
    return newGun
end

function gunStats:Weld(torso, bool)
    local ls,rs=torso["Left Shoulder"],torso["Right Shoulder"]
    local la,ra=torso.Parent["Left Arm"],torso.Parent["Right Arm"]  
    if bool then
        local arm = torso.Parent["Right Arm"]
        if self.Welding.WeldRightArm then
            rs.Part1=nil
            local weld = Instance.new("Weld", arm)
            weld.Part0 = torso
            weld.Part1 = weld.Parent
            weld.C1 = self.Welding.WeldRightArm --[[ Position of arm ]]--
        end
        arm = torso.Parent["Left Arm"]
        if self.Welding.WeldLeftArm then
            ls.Part1=nil 
            local weld = Instance.new("Weld", arm)
            weld.Part0 = torso
            weld.Part1 = weld.Parent
            weld.C1 = self.Welding.WeldLeftArm --[[ Position of arm]]--
        end
        local weld = Instance.new("Weld",script.Parent.PrimaryPart)
        weld.Part0= weld.Parent
        weld.Part1= torso.Parent["Left Arm"]
        weld.C1 = weld.C1 * CFrame.fromEulerAnglesXYZ(math.rad(90),math.rad(90),math.rad(90)) * CFrame.new(0,1,0)
    else
        for _, v in pairs(torso.Parent:GetChildren()) do
            if v.Name == "Left Arm" or v.Name == "Right Arm" and v:FindFirstChild("Weld") then
                v:FindFirstChild("Weld"):Destroy()
                ls.Part1=la
                rs.Part1=ra
            end
        end
    end
end


function gunStats:Fire(mouse)
    local function raycast(a,b)
        local ray=Ray.new(a,((a-b).Unit)*999)
        local hit,pos=workspace:FindPartOnRay(ray)
        return hit,pos 
    end

    local distance = (self.Barrel.Position - mouse).magnitude
    local spread=(self.MaxAccuracy)*(self.Recoil/100)+(self.Accuracy) 
    local aim=mouse+Vector3.new(
        math.random(-(spread/10)*distance,(spread/10)*distance),
        math.random(-(spread/10)*distance,(spread/10)*distance),
        math.random(-(spread/10)*distance,(spread/10)*distance)
    )
    local hit,pos=raycast(self.Barrel.Position,aim) 
    local b1=self.Bullet:clone()
    b1.Mesh.Scale=Vector3.new(b1.Mesh.Scale.X,b1.Mesh.Scale.Y,distance)
    b1.CFrame=CFrame.new(self.Barrel.Position, mouse) * CFrame.new(0, 0, -distance / 2)
b1.Parent=workspace:FindFirstChild("RayIgnore") and workspace["RayIgnore"] or error("No model named RayIgnore in workspace!")
game.Debris:AddItem(b1,.1)
end

return gunStats

为什么不直接创建一个变量来说明你是否可以射击?

装备时为真,未装备时为假。

当您通过 .new "constructor" 创建新的 gunStats -table 给它一个名为 "Equipped" 的布尔统计数据,并最初将其设置为 false。

调用 equip 方法时,将 "Equipped" 设置为 true。

我没有看到 unequip -方法,你一定要这样做,但你应该将 "Equipped" 设置为 false。

现在,当你开火时,你只需检查它是否已装备,如果是,则正常操作,如果不是,只需调用 return。 实际上,更好的方法是在 Button1Down 侦听器中调用 "Fire" 之前检查枪是否已装备,但这两种方式都是 acceptable.