2016-10-26 84 views
2

我正在製作一個不使用工具對象的槍引擎/系統。從本地腳本斷開模塊腳本

目前我擁有一切; 裝備 射擊 去裝備

這些槍是與他們的modulescripts模型。他們掌握着槍的統計數據和相關功能(射擊,焊接),但我碰到過一個問題

當玩家試圖裝備一支已經裝備好的槍時,他會去掉它(應該發生),但他仍然可以開除裝備不足的槍。射擊/射線投射來自槍的最後位置,然後才被裝備。

如果我再次裝備槍並開火,它會發射先前裝備的槍和配備的槍。

我該如何解決這個問題?

的裝備功能(本地腳本中)

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 

回答

2

爲什麼不只是做一個變量,說你是否可以拍攝或不?

配備時爲真,未配備時爲假。

+0

這必須是評論而不是答案。 – pix

+0

@pix不,這是一個很好的答案。 – warspyking

1

當您通過.new「構造函數」 創建一個新的gunStats -table時,給它一個名爲「EQUipped」的布爾值統計量,並將其初始化爲false。

當您調用equip -method時,將「Equipped」設置爲true。

我沒有看到一個你應該明確做出的非調整方法,但是你應該將「EQUipped」設置爲false。

現在,在發射武器時,你只需檢查它是否配備,如果是,則通常做一些事情,如果沒有,就調用返回。 實際上,更好的方法是在Button1Down監聽器中調用「Fire」之前檢查槍是否配備,但兩種方式都可以接受。