Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Class 断开modulescript与本地脚本的连接_Class_Lua_Roblox - Fatal编程技术网

Class 断开modulescript与本地脚本的连接

Class 断开modulescript与本地脚本的连接,class,lua,roblox,Class,Lua,Roblox,我正在制作一个不使用工具对象的枪引擎/系统 现在我把一切都搞定了; 装备 发射 解除装备 这些枪是带有模块说明的模型。他们掌握着枪支的统计数据和相关功能(射击、焊接),但我遇到了一个问题 当玩家试图装备已经装备的枪时,他会解除装备(这应该发生),但他仍然可以发射解除装备的枪。射程/光线投射来自火炮解除装备前的最后一个位置 如果我再次装备枪并开火,它会同时发射先前未装备的枪和装备的枪 我怎样才能解决这个问题 设备功能(在本地脚本中) 我正在测试的枪内的模块脚本: local gunStats =

我正在制作一个不使用工具对象的枪引擎/系统

现在我把一切都搞定了; 装备 发射 解除装备

这些枪是带有模块说明的模型。他们掌握着枪支的统计数据和相关功能(射击、焊接),但我遇到了一个问题

当玩家试图装备已经装备的枪时,他会解除装备(这应该发生),但他仍然可以发射解除装备的枪。射程/光线投射来自火炮解除装备前的最后一个位置

如果我再次装备枪并开火,它会同时发射先前未装备的枪和装备的枪

我怎样才能解决这个问题

设备功能(在本地脚本中)

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

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

为什么不做一个变量来说明你是否能射击


装备时为True,未装备时为false。

通过.new“构造函数”创建新的gunStats-表时 给它一个名为“装备”的布尔状态,并最初将其设置为false

调用equipment-method时,将“装备”设置为true

我没有看到一个不公平的方法,你肯定应该这样做,但是你应该把“装备”设置为false

现在,当你发射武器时,你只需检查它是否装备了,如果装备了,请正常操作,如果没有,请致电return。
实际上,更好的方法是在按下按钮的侦听器中发出“开火”之前检查枪是否配备,但这两种方法都是可以接受的。

这一定是一个注释而不是答案。@pix否,这是一个很好的答案。
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