Henderick
|
  |
| Joined: 10 Dec 2007 |
| Total Posts: 1523 |
|
|
| 26 Apr 2013 01:34 AM |
I'm trying to add recoil to a gun, but when you fire you don't stop firing and even after you stop firing, the recoil is still going.
The base I used for the gun is Ubayla's gun system.
Relevant functions in script:
function RecoilBack() welds[1].C0 = CFrame.new(-0.4, -1, -1.2) * CFrame.Angles(math.rad(90), 0, math.rad(40)) welds[2].C0 = CFrame.new(0.6, -1, -0.4) * CFrame.Angles(math.rad(90), 0, math.rad(-5)) welds[3].C0 = CFrame.new(-0.4, -1, -1.2) * CFrame.Angles(math.rad(90), 0, math.rad(40)) welds[4].C0 = CFrame.new(0.6, -1, -0.4) * CFrame.Angles(math.rad(90), 0, math.rad(-5)) wait() welds[1].C0 = CFrame.new(-0.4, -1, -1.1) * CFrame.Angles(math.rad(90), 0, math.rad(40)) welds[2].C0 = CFrame.new(0.6, -1, -0.3) * CFrame.Angles(math.rad(90), 0, math.rad(-5)) welds[3].C0 = CFrame.new(-0.4, -1, -1.1) * CFrame.Angles(math.rad(90), 0, math.rad(40)) welds[4].C0 = CFrame.new(0.6, -1, -0.3) * CFrame.Angles(math.rad(90), 0, math.rad(-5)) wait() welds[1].C0 = CFrame.new(-0.4, -1, -1) * CFrame.Angles(math.rad(90), 0, math.rad(40)) welds[2].C0 = CFrame.new(0.6, -1, -0.2) * CFrame.Angles(math.rad(90), 0, math.rad(-5)) welds[3].C0 = CFrame.new(-0.4, -1, -1) * CFrame.Angles(math.rad(90), 0, math.rad(40)) welds[4].C0 = CFrame.new(0.6, -1, -0.2) * CFrame.Angles(math.rad(90), 0, math.rad(-5)) end
============================================================== function Shoot(mouse) RecoilBack() if Stop == false and User.Humanoid.Health > 0 then if Firing == false then if menu == false and Reloading == false and sprinting == false and throwing == false and swinging == false then for i=1, Bullets do if not (Ammo.Value <= 0) then if Shot == false then Ammo.Value = Ammo.Value - 1 end Firing = true local TargetPoint = (User.Head.CFrame * CFrame.new(0, 0, -Limit)).p local Start = (Tool.Handle.CFrame * CFrame.new(0, Tool.Handle.Size.y/2, 0)).p local Dist1 = (TargetPoint - Start).magnitude local Flaw = Vector3.new(Recoil * ((math.random() * 2) - 1), Recoil * ((math.random() * 2) - 1), Recoil * ((math.random() * 2) - 1)) * Dist1 * 0.25 local FinalTarget = (TargetPoint + Flaw) local NewRay = Ray(Start, (FinalTarget - Start).unit * Limit, User) local hit, RayPos = game.Workspace:FindPartOnRay(NewRay, User, true) local Dist2 = (RayPos - Start).magnitude if Shot == false then FireSound:play() end if Trail then local BulletPart = NewInstance("Part",Tool) BulletPart.Name = "Bullet" BulletPart.BrickColor = TrailColor BulletPart.Transparency = 0.9 BulletPart.Anchored = true BulletPart.CanCollide = false BulletPart.TopSurface = Enum.SurfaceType.Smooth BulletPart.BottomSurface = Enum.SurfaceType.Smooth BulletPart.formFactor = Enum.FormFactor.Custom BulletPart.Size = NewVector3(0.5, Dist2, 0.5) BulletPart.CFrame = (NewCFrame(RayPos, Start) * NewCFrame(0, 0, -Dist2/2)) * CFrame.Angles(0, math.pi/2, math.pi/2) local BulletMesh = NewInstance("CylinderMesh", BulletPart) BulletMesh.Scale = NewVector3(0.2, 1, 0.2) game.Debris:AddItem(BulletPart, 0.1) end if not Shot then Recoil = Recoil + RecoilInc end Cam = game.Workspace.CurrentCamera local CamAngle = Cam.CoordinateFrame - Cam.CoordinateFrame.p local CamZoom = (Cam.CoordinateFrame.p - Cam.Focus.p).magnitude local NewCam = CFrame.new(Cam.Focus.p) * CamAngle * CFrame.Angles(math.random(-2, 2)/Division, math.random(-2, 2)/Division, 0) Cam.CoordinateFrame = NewCam * CFrame.new(0, 0, CamZoom) if hit then if hit.Parent then if hit.Parent:findFirstChild("Humanoid") and hit.Parent ~= Player.Character then if game.Players:GetPlayerFromCharacter(hit.Parent) then if game.Players:GetPlayerFromCharacter(hit.Parent).TeamColor ~= Player.TeamColor or game.Players:GetPlayerFromCharacter(hit.Parent).Neutral == true then local Damage = math.random(BulletDamage - 5, BulletDamage + 5) hit.Parent.Humanoid:TakeDamage(Damage)
local NewForce = Instance.new("BodyForce") NewForce.Name = "BulletForce" NewForce.Parent = hit NewForce.force = Player.Character.Torso.CFrame.lookVector * 5000 game.Debris:AddItem(NewForce, 1/30)
local NewPart = Instance.new("Part") NewPart.Parent = Tool NewPart.Anchored = true NewPart.CanCollide = false NewPart.FormFactor = "Custom" if Hole then NewPart.Transparency = 0.9 else NewPart.Transparency = 1 end NewPart.BrickColor = BrickColor.new("Black") NewPart.Size = Vector3.new(0.2, 0.2 ,0.2) NewPart.CFrame = CFrame.new(RayPos.x, RayPos.y, RayPos.z) local NewSound = HitSound:Clone() local PitchVariable = (math.random()/2) + 0.75 NewSound.Pitch = PitchVariable NewSound.Parent = NewPart game.Debris:AddItem(NewSound, 1) game.Debris:AddItem(NewPart, 1) NewSound:Play()
local NewCreator = Tool.creator:Clone() NewCreator.Value = Player NewCreator.Parent = hit.Parent game.Debris:AddItem(NewCreator, 1)
HitTarget(Player) end else local Damage = math.random(BulletDamage - 5, BulletDamage + 5) hit.Parent.Humanoid:TakeDamage(Damage)
local NewForce = Instance.new("BodyForce") NewForce.Name = "BulletForce" NewForce.Parent = hit NewForce.force = Player.Character.Torso.CFrame.lookVector * 5000 game.Debris:AddItem(NewForce, 1/30)
local NewPart = Instance.new("Part") NewPart.Parent = Tool NewPart.Anchored = true if Hole then NewPart.Transparency = 0.9 else NewPart.Transparency = 1 end NewPart.BrickColor = BrickColor.new("Black") NewPart.CanCollide = false NewPart.FormFactor = "Custom" NewPart.Size = Vector3.new(0.2, 0.2 ,0.2) NewPart.CFrame = CFrame.new(RayPos.x, RayPos.y, RayPos.z) local NewSound = HitSound:Clone() local PitchVariable = (math.random()/2) + 0.75 NewSound.Pitch = PitchVariable NewSound.Parent = NewPart game.Debris:AddItem(NewSound, 1) game.Debris:AddItem(NewPart, 1) NewSound:Play()
local NewCreator = Tool.creator:Clone() NewCreator.Value = Player NewCreator.Parent = hit.Parent game.Debris:AddItem(NewCreator, 1)
HitTarget(Player) end elseif hit.Parent.Parent:findFirstChild("Humanoid") then if game.Players:GetPlayerFromCharacter(hit.Parent.Parent) then if game.Players:GetPlayerFromCharacter(hit.Parent.Parent).TeamColor ~= Player.TeamColor or game.Players:GetPlayerFromCharacter(hit.Parent.Parent).Neutral == true then local Damage = math.random(BulletDamage - 5, BulletDamage + 5) hit.Parent.Parent.Humanoid:TakeDamage(Damage)
local NewForce = Instance.new("BodyForce") NewForce.Name = "BulletForce" NewForce.Parent = hit NewForce.force = Player.Character.Torso.CFrame.lookVector * 5000 game.Debris:AddItem(NewForce, 1/30)
local NewPart = Instance.new("Part") NewPart.Parent = Tool NewPart.Anchored = true NewPart.CanCollide = false NewPart.FormFactor = "Custom" if Hole then NewPart.Transparency = 0.9 else NewPart.Transparency = 1 end NewPart.BrickColor = BrickColor.new("Black") NewPart.Size = Vector3.new(0.2, 0.2 ,0.2) NewPart.CFrame = CFrame.new(RayPos.x, RayPos.y, RayPos.z) local NewSound = HitSound:Clone() local PitchVariable = (math.random()/2) + 0.75 NewSound.Pitch = PitchVariable NewSound.Parent = NewPart game.Debris:AddItem(NewSound, 1) game.Debris:AddItem(NewPart, 1) NewSound:Play()
local NewCreator = Tool.creator:Clone() NewCreator.Value = Player NewCreator.Parent = hit.Parent game.Debris:AddItem(NewCreator, 1)
HitTarget(Player) end else local Damage = math.random(BulletDamage - 5, BulletDamage + 5) hit.Parent.Parent.Humanoid:TakeDamage(Damage)
local NewForce = Instance.new("BodyForce") NewForce.Name = "BulletForce" NewForce.Parent = hit NewForce.force = Person.Character.Torso.CFrame.lookVector * 5000 game.Debris:AddItem(NewForce, 1/30)
local NewPart = Instance.new("Part") NewPart.Parent = Tool NewPart.Anchored = true NewPart.CanCollide = false NewPart.FormFactor = "Custom" if Hole then NewPart.Transparency = 0.9 else NewPart.Transparency = 1 end NewPart.BrickColor = BrickColor.new("Black") NewPart.Size = Vector3.new(0.2, 0.2 ,0.2) NewPart.CFrame = CFrame.new(RayPos.x, RayPos.y, RayPos.z) local NewSound = HitSound:Clone() local PitchVariable = (math.random()/2) + 0.75 NewSound.Pitch = PitchVariable NewSound.Parent = NewPart game.Debris:AddItem(NewSound, 1) game.Debris:AddItem(NewPart, 1) NewSound:Play()
local NewCreator = Tool.creator:Clone() NewCreator.Value = Player NewCreator.Parent = hit.Parent game.Debris:AddItem(NewCreator, 1)
HitTarget(Player) end else local NewForce = Instance.new("BodyForce") NewForce.Name = "BulletForce" NewForce.Parent = hit NewForce.force = Player.Character.Torso.CFrame.lookVector * 5000 game.Debris:AddItem(NewForce, 1/30)
local NewPart = Instance.new("Part") NewPart.Parent = Tool NewPart.Anchored = true NewPart.CanCollide = false NewPart.FormFactor = "Custom" if Hole then NewPart.Transparency = 0.9 else NewPart.Transparency = 1 end NewPart.BrickColor = BrickColor.new("Black") NewPart.Size = Vector3.new(0.2, 0.2 ,0.2) NewPart.CFrame = CFrame.new(RayPos.x, RayPos.y, RayPos.z) local NewSound = MissSound:Clone() local PitchVariable = (math.random()/2.5) + 0.75 NewSound.Pitch = PitchVariable NewSound.Parent = NewPart game.Debris:AddItem(NewSound, 0.5) game.Debris:AddItem(NewPart, 0.5) NewSound:Play() end end end MoveArms(M) if Burst == true then wait(0.1) end else EmptySound:Play() end end if Shot == true then if not (Ammo.Value <= 0 ) then Ammo.Value = Ammo.Value - 1 Recoil = Recoil + (RecoilInc * Bullets) FireSound:Play() end delay(Rate, function () Firing = false end) end if Rapid == true then wait(Rate) Firing = false Shoot(mouse) elseif Burst == true then wait(Rate) Firing = false end end end end end
===============================================================
You can see the error for yourself at this place: http://www.roblox.com/testing-place?id=25135976
The gun is "TMP EDITED". I think you have to kill yourself once for the gun to work properly.
Thank you in advance!
|
|
|
| Report Abuse |
|