| |
|
»
»
|
|
| |
Re: Zombies unkillable
|
|
|
|
| 16 Mar 2014 06:52 PM |
I recently added teams into my game Survivors and Spectating, but if you're on survivors or spectating, you can't kill zombies with the starting gun, so I was wondering if anyone was kind enough to help me fix it.
local ai = {} ai.Model = script.Parent
--ai memory local goalPos local lastAttack = time() local attackTime = 1 local atkDamage = 19 local goalHumanoid local running local running2 local crawling local limp = {} for i = 1, 10 do limp[i] = (math.random() * 2) - 1 end
--global functions ai.Move = function(loc, mustSee, humanoid) goalHumanoid = humanoid if goalPos then goalPos = loc else goalPos = loc Spawn(function() ai.State = "Tracking" local lastGoal local lastTime = time() while goalPos and ai.Humanoid do local gp = goalPos if pcall(function() return goalPos.X end) then elseif goalPos:IsA("BasePart") then gp = (ai.canSee(goalPos) or not mustSee) and goalPos.Position or nil lastTime = time() elseif goalPos:IsA("Model") then gp = nil for _, bodyPart in pairs(goalPos:GetChildren()) do if bodyPart:IsA("BasePart") and (ai.canSee(bodyPart) or not mustSee) then gp = goalPos:GetModelCFrame().p lastTime = time() break end end end lastGoal = gp and humanoid and gp + (gp - ai.Model.Torso.Position).unit * 10 or gp or lastGoal or ai.Model.Torso.Position if (goalHumanoid and goalHumanoid.Health <= 0) or (not goalHumanoid and (ai.Model.Torso.Position - lastGoal).magnitude < 3) or time() - lastTime > 3 then goalPos = nil else ai.Humanoid.Jump = objAhead(lastGoal) ai.Humanoid:MoveTo(lastGoal, workspace.Terrain) if goalHumanoid and ((ai.Model.Torso.Position - goalPos:GetModelCFrame().p) * Vector3.new(1,.5,1)).magnitude < 3 and time() - lastAttack > attackTime then attack(goalHumanoid) end end wait(.1) end if ai then ai.State = "Idle" end end) end end
--local functions function weld(a, b, c0, c1) local w = Instance.new("Weld", a) w.Part0 = a w.Part1 = b if c0 then w.C0 = c0 end if c1 then w.C1 = c1 end return w end
function setHeight(n) if ai.Model:findFirstChild("Right Leg") then ai.Model["Right Leg"]:BreakJoints() ai.Model["Right Leg"].Size = ai.Model["Right Leg"].Size * Vector3.new(1,0,1) + Vector3.new(0, math.max(0.2, n or 0),0) weld(ai.Model.Torso, ai.Model["Right Leg"], CFrame.new(0, -n + ai.Model["Right Leg"].Size.Y/2 - ai.Model.Torso.Size.Y/2,0)) end if m and ai.Model:findFirstChild("Head") then ai.Model.Head:BreakJoints() weld(ai.Model.Torso, ai.Model.Head, CFrame.new(0, m - ai.Model.Head.Size.Y/2 + ai.Model.Torso.Size.Y/2,0)) end end
function objAhead(loc) if not _G.AntiJumpCache then _G.AntiJumpCache = {} end local ray = Ray.new(ai.Model.Torso.Position, ((loc - ai.Model.Torso.Position) * Vector3.new(1,0,1)).unit * 5) local obj, pos = workspace:FindPartOnRayWithIgnoreList(ray, _G.AntiJumpCache) if obj then if not obj.CanCollide then table.insert(_G.AntiJumpCache, obj) elseif obj.Parent:findFirstChild("Humanoid") then table.insert(_G.AntiJumpCache, obj.Parent) else return true end --add to cache if necessary, otherwise recurse return objAhead(loc) end end
function ai.canSee(obj, fov) local direction = (obj.Position - ai.Model.Head.Position).unit if fov and direction:Dot(ai.Model.Head.CFrame.lookVector) < math.cos(math.rad(fov)) then return false end local ray = Ray.new(ai.Model.Head.Position, direction * 900) local hit, pos = workspace:FindPartOnRayWithIgnoreList(ray, {ai.Model}) if hit then --add to cache if transparent? if hit == obj then return true end end end
function attack(hum) print("pow") lastAttack = time() hum:TakeDamage(atkDamage) end
function initialize() for _, obj in pairs(ai.Model:GetChildren()) do if obj:IsA("BasePart") then obj.Anchored = false end end setHeight(2, 1) ai.tWeld = weld(ai.Model.Torso, ai.Model.torso, CFrame.new(0,-1,0), CFrame.new(0,-1,0) * CFrame.Angles(math.rad(45),0,0)) ai.hWeld = weld(ai.Model.torso, ai.Model.head2, CFrame.new(0,.8,0), CFrame.new(0,-.6,0) * CFrame.Angles(math.rad(-22.5),0,0)) ai.lWeld1 = weld(ai.Model.torso, ai.Model.leftLeg, CFrame.new(-0.5,-1,0), CFrame.new(0,1,0) * CFrame.Angles(math.rad(-45),0,0)) ai.lWeld2 = weld(ai.Model.torso, ai.Model.rightLeg, CFrame.new(0.5,-1,0), CFrame.new(0,1,0) * CFrame.Angles(math.rad(-45),0,0)) ai.aWeld1 = weld(ai.Model.torso, ai.Model.leftArm, CFrame.new(-1,0.5,0), CFrame.new(0.5,0.5,0) * CFrame.Angles(math.rad(-45),0,0)) ai.aWeld2 = weld(ai.Model.torso, ai.Model.rightArm, CFrame.new(1,0.5,0), CFrame.new(-0.5,0.5,0) * CFrame.Angles(math.rad(-45),0,0)) ai.HWeld = weld(ai.Model.Torso, ai.Model.Head) anim(math.rad(20),0,0) ai.Humanoid = Instance.new("Humanoid", ai.Model) ai.Humanoid.WalkSpeed = math.random(8,14) + math.max(0, math.random(-5,3)) ai.State = "Idle" ai.Model.ChildRemoved:connect(delete) ai.Humanoid.Died:connect(delete) ai.Humanoid.Running:connect(animate) table.insert(_G.aiTable, ai) end
function delete() wait(0) if not ai.Humanoid or not (ai.Humanoid.Health > 0) or not ai.Model:findFirstChild("Torso") or not ai.Model:FindFirstChild("Head") or not ai.Model:FindFirstChild("head2") then for i, a in pairs(_G.aiTable) do if a == ai then table.remove(_G.aiTable, i) end end ai.Model:BreakJoints() game.Debris:AddItem(ai.Model, 3) for i, obj in pairs(ai.Model:GetChildren()) do if obj:IsA("BasePart") and obj.Transparency ~= 1 then obj.CanCollide = true elseif obj ~= script then obj:Destroy() end end ai = nil goalPos = nil elseif not (ai.Model:FindFirstChild("leftLeg") and ai.Model:FindFirstChild("rightLeg")) and not crawling then crawl() end if ai and crawling and not (ai.Model:FindFirstChild("leftArm") and ai.Model:FindFirstChild("rightArm")) then ai.Humanoid.Health = 0 end end
function animate(a) if not running2 then running = a > 0 if running then Spawn(function() running2 = true local start = time() while running do wait(0) local cycle = (tick() - start) * 5 local tPitch = math.rad(25 + math.cos(cycle * 2) * -5) local tRoll = math.rad(math.sin(cycle) * 10) local lPitch = math.rad(math.sin(cycle) * 45) anim(tPitch, tRoll, lPitch) end running2 = false anim(math.rad(20),0,0) end) end else running = a > 0 end end
function anim(tPitch, tRoll, lPitch) if not ai then return end local crawlMod = crawling and math.rad(50) or 0 local crawlMod2 = crawling and 4 or 1 local attackMod = (attackTime - math.min(attackTime,(time() - lastAttack)))/attackTime * math.rad(140) lPitch2 = crawling and lPitch / 3 or lPitch if ai.tWeld then ai.tWeld.C1 = CFrame.new(0,-1,0) * CFrame.Angles(tPitch + crawlMod,0,tRoll + limp[1] * .4) end if ai.hWeld then ai.hWeld.C1 = CFrame.new(0,-0.6,0) * CFrame.Angles(-tPitch/2,0,tRoll + limp[2] * .4) end if ai.lWeld1 then ai.lWeld1.C1 = CFrame.new(0,1,0) * CFrame.Angles(lPitch2 - tPitch + crawlMod + limp[3] * .4,0,-tRoll - limp[1] * .4) end if ai.lWeld2 then ai.lWeld2.C1 = CFrame.new(0,1,0) * CFrame.Angles(-lPitch2 - tPitch + crawlMod + limp[4] * .4,0,-tRoll - limp[1] * .4) end if ai.aWeld1 then ai.aWeld1.C1 = CFrame.new(0.5,0.5,0) * CFrame.Angles(0,0, crawlMod/2) * CFrame.Angles(lPitch * math.abs(limp[5]) * -0.4 * crawlMod2 - tPitch - crawlMod - attackMod + limp[5] * .2, 0, -tRoll - limp[1] * .4) end if ai.aWeld2 then ai.aWeld2.C1 = CFrame.new(-0.5,0.5,0) * CFrame.Angles(0,0, -crawlMod/2) * CFrame.Angles(lPitch * math.abs(limp[6]) * 0.4 * crawlMod2 - tPitch - crawlMod - attackMod + limp[6] * .2, 0, -tRoll - limp[1] * .4) end if ai.HWeld and ai.Model:findFirstChild("head2") then ai.HWeld.C0 = ai.Model.Torso.CFrame:toObjectSpace(ai.Model.head2.CFrame) end end
function crawl() crawling = true setHeight(0,0) ai.Humanoid.WalkSpeed = math.random(4, 6) end
while not _G.aiTable do wait(0) end initialize()
If you need the gun script, just ask.
ravioli ravioli give me the formuoli |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:11 PM |
I would suggest making the Humanoid into Zombie. Then from there you can make the gun script Instead of (Humanoid) You make it into (zombie) |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:12 PM |
| Oh my god I saw you on a thread knowing nothing and then you think of this geniousness |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:15 PM |
@Sen
Im just trying to help :c |
|
|
| Report Abuse |
|
|
| |
|
|
| 16 Mar 2014 07:17 PM |
Oh xD Im use to getting called at by OT ... |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:17 PM |
@Initial, How exactly would I make it a zombie instead of humanoid?
ravioli ravioli give me the formuoli |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:18 PM |
its ok just remember
never cry to drake |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:19 PM |
1) Make the gun that cant kill zombies target humanoids named anything but "Zombie 2) Zombie's humanoids must be named "Zombie" 3) Weapons that can kill zombies should target humanoids named "Zombies"
hav ey neic dei |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:19 PM |
First of all if your zombie model doesnt have a Humanoid instance inside it it cant be killed
You have to rename it to Zombie Then PM me the gun script and ill try to see if I can edit it |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:23 PM |
@Inital, Here's the gun script.
--------------------- TEMPLATE STARTER PISTOL WEAPON --------------------------- -- Waits for the child of the specified parent local function WaitForChild(parent, childName) while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end return parent[childName] end
----- MAGIC NUMBERS ABOUT THE TOOL ----- -- How much damage a bullet does local Damage = 25 -- How many times per second the gun can fire local FireRate = 10 / 30 -- The maximum distance the can can shoot, this value should never go above 1000 local Range = 400 -- In radians the minimum accuracy penalty local MinSpread = 0.01 -- In radian the maximum accuracy penalty local MaxSpread = 0.1 -- Number of bullets in a clip local ClipSize = 8 -- DefaultValue for spare ammo local SpareAmmo = 900 -- The amount the aim will increase or decrease by -- decreases this number reduces the speed that recoil takes effect local AimInaccuracyStepAmount = 0.0125 -- Time it takes to reload weapon local ReloadTime = 1.5 ----------------------------------------
-- Colors local FriendlyReticleColor = Color3.new(0, 1, 0) local EnemyReticleColor = Color3.new(1, 0, 0) local NeutralReticleColor = Color3.new(1, 1, 1)
local Spread = MinSpread local AmmoInClip = ClipSize
local Tool = script.Parent local Handle = WaitForChild(Tool, 'Handle') local WeaponGui = nil
local LeftButtonDown local Reloading = false local IsShooting = false
-- Player specific convenience variables local MyPlayer = nil local MyCharacter = nil local MyHumanoid = nil local MyTorso = nil local MyMouse = nil
local RecoilAnim local RecoilTrack = nil
local IconURL = Tool.TextureId -- URL to the weapon icon asset
local DebrisService = game:GetService('Debris') local PlayersService = game:GetService('Players')
local FireSound
local OnFireConnection = nil local OnReloadConnection = nil
local DecreasedAimLastShot = false local LastSpreadUpdate = time()
-- this is a dummy object that holds the flash made when the gun is fired local FlashHolder = nil
local WorldToCellFunction = Workspace.Terrain.WorldToCellPreferSolid local GetCellFunction = Workspace.Terrain.GetCell
function RayIgnoreCheck(hit, pos) if hit then if hit.Transparency >= 1 or string.lower(hit.Name) == "water" or hit.Name == "Effect" or hit.Name == "Rocket" or hit.Name == "Bullet" or hit.Name == "Handle" or hit:IsDescendantOf(MyCharacter) then return true elseif hit:IsA('Terrain') and pos then local cellPos = WorldToCellFunction(Workspace.Terrain, pos) if cellPos then local cellMat = GetCellFunction(Workspace.Terrain, cellPos.x, cellPos.y, cellPos.z) if cellMat and cellMat == Enum.CellMaterial.Water then return true end end end end return false end
-- @preconditions: vec should be a unit vector, and 0 < rayLength <= 1000 function RayCast(startPos, vec, rayLength) local hitObject, hitPos = game.Workspace:FindPartOnRay(Ray.new(startPos + (vec * .01), vec * rayLength), Handle) if hitObject and hitPos then local distance = rayLength - (hitPos - startPos).magnitude if RayIgnoreCheck(hitObject, hitPos) and distance > 0 then -- there is a chance here for potential infinite recursion return RayCast(hitPos, vec, distance) end end return hitObject, hitPos end
function TagHumanoid(humanoid, player) -- Add more tags here to customize what tags are available. while humanoid:FindFirstChild('creator') do humanoid:FindFirstChild('creator'):Destroy() end local creatorTag = Instance.new("ObjectValue") creatorTag.Value = player creatorTag.Name = "creator" creatorTag.Parent = humanoid DebrisService:AddItem(creatorTag, 1.5)
local weaponIconTag = Instance.new("StringValue") weaponIconTag.Value = IconURL weaponIconTag.Name = "icon" weaponIconTag.Parent = creatorTag end
local function CreateFlash() if FlashHolder then local flash = Instance.new('Fire', FlashHolder) flash.Color = Color3.new(1, 140 / 255, 0) flash.SecondaryColor = Color3.new(1, 0, 0) flash.Size = 0.3 DebrisService:AddItem(flash, FireRate / 1.5) else FlashHolder = Instance.new("Part", Tool) FlashHolder.Transparency = 1 FlashHolder.CanCollide= false FlashHolder.Size = Vector3.new(1, 1, 1) FlashHolder.Position = Tool.Handle.Position local Weld = Instance.new("ManualWeld") Weld.C0 = CFrame.new(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1) Weld.C1 = CFrame.new(0, 2.2, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0) Weld.Part0 = FlashHolder Weld.Part1 = Tool.Handle Weld.Parent = FlashHolder end end
local function CreateBullet(bulletPos) local bullet = Instance.new('Part', Workspace) bullet.FormFactor = Enum.FormFactor.Custom bullet.Size = Vector3.new(0.1, 0.1, 0.1) bullet.BrickColor = MyPlayer.TeamColor bullet.Shape = Enum.PartType.Ball bullet.CanCollide = false bullet.CFrame = CFrame.new(bulletPos) bullet.Anchored = true bullet.TopSurface = Enum.SurfaceType.Smooth bullet.BottomSurface = Enum.SurfaceType.Smooth bullet.Name = 'Bullet' DebrisService:AddItem(bullet, 2.5) local fire = Instance.new("Fire", bullet) fire.Color = Color3.new(MyPlayer.TeamColor.r, MyPlayer.TeamColor.g, MyPlayer.TeamColor.b) fire.SecondaryColor = Color3.new(MyPlayer.TeamColor.r, MyPlayer.TeamColor.g, MyPlayer.TeamColor.b) fire.Size = 5 fire.Heat = 0 DebrisService:AddItem(fire, 0.2) return bullet end
local function Reload() if not Reloading then Reloading = true -- Don't reload if you are already full or have no extra ammo if AmmoInClip ~= ClipSize and SpareAmmo > 0 then if RecoilTrack then RecoilTrack:Stop() end if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then WeaponGui.Crosshair.ReloadingLabel.Visible = true end end wait(ReloadTime) -- Only use as much ammo as you have local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo) AmmoInClip = AmmoInClip + ammoToUse SpareAmmo = SpareAmmo - ammoToUse UpdateAmmo(AmmoInClip) end Reloading = false end end
function OnFire() if IsShooting then return end if MyHumanoid and MyHumanoid.Health > 0 then if RecoilTrack and AmmoInClip > 0 then RecoilTrack:Play() end IsShooting = true while LeftButtonDown and AmmoInClip > 0 and not Reloading do if Spread and not DecreasedAimLastShot then Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount) UpdateCrosshair(Spread) end DecreasedAimLastShot = not DecreasedAimLastShot if Handle:FindFirstChild('FireSound') then Handle.FireSound:Play() end CreateFlash() if MyMouse then local targetPoint = MyMouse.Hit.p local shootDirection = (targetPoint - Handle.Position).unit -- Adjust the shoot direction randomly off by a little bit to account for recoil shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread, (0.5 - math.random()) * 2 * Spread, (0.5 - math.random()) * 2 * Spread) * shootDirection local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range) local bullet -- Create a bullet here if hitObject then bullet = CreateBullet(bulletPos) end if hitObject and hitObject.Parent then local hitHumanoid = hitObject.Parent:FindFirstChild("Humanoid") if hitHumanoid then local hitPlayer = game.Players:GetPlayerFromCharacter(hitHumanoid.Parent) if MyPlayer.Neutral or (hitPlayer and hitPlayer.TeamColor ~= MyPlayer.TeamColor) then TagHumanoid(hitHumanoid, MyPlayer) hitHumanoid:TakeDamage(Damage) if bullet then bullet:Destroy() bullet = nil --bullet.Transparency = 1 end Spawn(UpdateTargetHit) end end end AmmoInClip = AmmoInClip - 1 UpdateAmmo(AmmoInClip) end wait(FireRate) end IsShooting = false if AmmoInClip == 0 then Reload() end if RecoilTrack then RecoilTrack:Stop() end end end
local TargetHits = 0 function UpdateTargetHit() TargetHits = TargetHits + 1 if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then WeaponGui.Crosshair.TargetHitImage.Visible = true end wait(0.5) TargetHits = TargetHits - 1 if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then WeaponGui.Crosshair.TargetHitImage.Visible = false end end
function UpdateCrosshair(value, mouse) if WeaponGui then local absoluteY = 650 WeaponGui.Crosshair:TweenSize( UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.33) end end
function UpdateAmmo(value) if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then WeaponGui.Crosshair.ReloadingLabel.Visible = false end end if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo end end
function OnMouseDown() LeftButtonDown = true OnFire() end
function OnMouseUp() LeftButtonDown = false end
function OnKeyDown(key) if string.lower(key) == 'r' then Reload() end end
function OnEquipped(mouse) RecoilAnim = WaitForChild(Tool, 'Recoil') FireSound = WaitForChild(Handle, 'FireSound')
MyCharacter = Tool.Parent MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter) MyHumanoid = MyCharacter:FindFirstChild('Humanoid') MyTorso = MyCharacter:FindFirstChild('Torso') MyMouse = mouse WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone() if WeaponGui and MyPlayer then WeaponGui.Parent = MyPlayer.PlayerGui UpdateAmmo(AmmoInClip) end if RecoilAnim then RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim) end
if MyMouse then -- Disable mouse icon MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154" MyMouse.Button1Down:connect(OnMouseDown) MyMouse.Button1Up:connect(OnMouseUp) MyMouse.KeyDown:connect(OnKeyDown) end end
-- Unequip logic here function OnUnequipped() LeftButtonDown = false Reloading = false MyCharacter = nil MyHumanoid = nil MyTorso = nil MyPlayer = nil MyMouse = nil if OnFireConnection then OnFireConnection:disconnect() end if OnReloadConnection then OnReloadConnection:disconnect() end if FlashHolder then FlashHolder = nil end if WeaponGui then WeaponGui.Parent = nil WeaponGui = nil end if RecoilTrack then RecoilTrack:Stop() end end
local function SetReticleColor(color) if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do if line:IsA('Frame') then line.BorderColor3 = color end end end end
Tool.Equipped:connect(OnEquipped) Tool.Unequipped:connect(OnUnequipped)
while true do wait(0.033) if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y) SetReticleColor(NeutralReticleColor)
local target = MyMouse.Target if target and target.Parent then local player = PlayersService:GetPlayerFromCharacter(target.Parent) if player then if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then SetReticleColor(EnemyReticleColor) else SetReticleColor(FriendlyReticleColor) end end end end if Spread and not IsShooting then local currTime = time() if currTime - LastSpreadUpdate > FireRate * 2 then LastSpreadUpdate = currTime Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount) UpdateCrosshair(Spread, MyMouse) end end end
ravioli ravioli give me the formuoli |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:30 PM |
Make the Humanoid for your zombie model "Zombie" simply by renaming it
--------------------- TEMPLATE STARTER PISTOL WEAPON --------------------------- -- Waits for the child of the specified parent local function WaitForChild(parent, childName) while not parent:FindFirstChild(childName) do parent.ChildAdded:wait() end return parent[childName] end
----- MAGIC NUMBERS ABOUT THE TOOL ----- -- How much damage a bullet does local Damage = 25 -- How many times per second the gun can fire local FireRate = 10 / 30 -- The maximum distance the can can shoot, this value should never go above 1000 local Range = 400 -- In radians the minimum accuracy penalty local MinSpread = 0.01 -- In radian the maximum accuracy penalty local MaxSpread = 0.1 -- Number of bullets in a clip local ClipSize = 8 -- DefaultValue for spare ammo local SpareAmmo = 900 -- The amount the aim will increase or decrease by -- decreases this number reduces the speed that recoil takes effect local AimInaccuracyStepAmount = 0.0125 -- Time it takes to reload weapon local ReloadTime = 1.5 ----------------------------------------
-- Colors local FriendlyReticleColor = Color3.new(0, 1, 0) local EnemyReticleColor = Color3.new(1, 0, 0) local NeutralReticleColor = Color3.new(1, 1, 1)
local Spread = MinSpread local AmmoInClip = ClipSize
local Tool = script.Parent local Handle = WaitForChild(Tool, 'Handle') local WeaponGui = nil
local LeftButtonDown local Reloading = false local IsShooting = false
-- Player specific convenience variables local MyPlayer = nil local MyCharacter = nil local MyZombie = nil local MyTorso = nil local MyMouse = nil
local RecoilAnim local RecoilTrack = nil
local IconURL = Tool.TextureId -- URL to the weapon icon asset
local DebrisService = game:GetService('Debris') local PlayersService = game:GetService('Players')
local FireSound
local OnFireConnection = nil local OnReloadConnection = nil
local DecreasedAimLastShot = false local LastSpreadUpdate = time()
-- this is a dummy object that holds the flash made when the gun is fired local FlashHolder = nil
local WorldToCellFunction = Workspace.Terrain.WorldToCellPreferSolid local GetCellFunction = Workspace.Terrain.GetCell
function RayIgnoreCheck(hit, pos) if hit then if hit.Transparency >= 1 or string.lower(hit.Name) == "water" or hit.Name == "Effect" or hit.Name == "Rocket" or hit.Name == "Bullet" or hit.Name == "Handle" or hit:IsDescendantOf(MyCharacter) then return true elseif hit:IsA('Terrain') and pos then local cellPos = WorldToCellFunction(Workspace.Terrain, pos) if cellPos then local cellMat = GetCellFunction(Workspace.Terrain, cellPos.x, cellPos.y, cellPos.z) if cellMat and cellMat == Enum.CellMaterial.Water then return true end end end end return false end
-- @preconditions: vec should be a unit vector, and 0 < rayLength <= 1000 function RayCast(startPos, vec, rayLength) local hitObject, hitPos = game.Workspace:FindPartOnRay(Ray.new(startPos + (vec * .01), vec * rayLength), Handle) if hitObject and hitPos then local distance = rayLength - (hitPos - startPos).magnitude if RayIgnoreCheck(hitObject, hitPos) and distance > 0 then -- there is a chance here for potential infinite recursion return RayCast(hitPos, vec, distance) end end return hitObject, hitPos end
function TagZombie(Zombie, player) -- Add more tags here to customize what tags are available. while Zombie:FindFirstChild('creator') do Zombie:FindFirstChild('creator'):Destroy() end local creatorTag = Instance.new("ObjectValue") creatorTag.Value = player creatorTag.Name = "creator" creatorTag.Parent = Zombie DebrisService:AddItem(creatorTag, 1.5)
local weaponIconTag = Instance.new("StringValue") weaponIconTag.Value = IconURL weaponIconTag.Name = "icon" weaponIconTag.Parent = creatorTag end
local function CreateFlash() if FlashHolder then local flash = Instance.new('Fire', FlashHolder) flash.Color = Color3.new(1, 140 / 255, 0) flash.SecondaryColor = Color3.new(1, 0, 0) flash.Size = 0.3 DebrisService:AddItem(flash, FireRate / 1.5) else FlashHolder = Instance.new("Part", Tool) FlashHolder.Transparency = 1 FlashHolder.CanCollide= false FlashHolder.Size = Vector3.new(1, 1, 1) FlashHolder.Position = Tool.Handle.Position local Weld = Instance.new("ManualWeld") Weld.C0 = CFrame.new(0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1) Weld.C1 = CFrame.new(0, 2.2, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0) Weld.Part0 = FlashHolder Weld.Part1 = Tool.Handle Weld.Parent = FlashHolder end end
local function CreateBullet(bulletPos) local bullet = Instance.new('Part', Workspace) bullet.FormFactor = Enum.FormFactor.Custom bullet.Size = Vector3.new(0.1, 0.1, 0.1) bullet.BrickColor = MyPlayer.TeamColor bullet.Shape = Enum.PartType.Ball bullet.CanCollide = false bullet.CFrame = CFrame.new(bulletPos) bullet.Anchored = true bullet.TopSurface = Enum.SurfaceType.Smooth bullet.BottomSurface = Enum.SurfaceType.Smooth bullet.Name = 'Bullet' DebrisService:AddItem(bullet, 2.5) local fire = Instance.new("Fire", bullet) fire.Color = Color3.new(MyPlayer.TeamColor.r, MyPlayer.TeamColor.g, MyPlayer.TeamColor.b) fire.SecondaryColor = Color3.new(MyPlayer.TeamColor.r, MyPlayer.TeamColor.g, MyPlayer.TeamColor.b) fire.Size = 5 fire.Heat = 0 DebrisService:AddItem(fire, 0.2) return bullet end
local function Reload() if not Reloading then Reloading = true -- Don't reload if you are already full or have no extra ammo if AmmoInClip ~= ClipSize and SpareAmmo > 0 then if RecoilTrack then RecoilTrack:Stop() end if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then if WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then WeaponGui.Crosshair.ReloadingLabel.Visible = true end end wait(ReloadTime) -- Only use as much ammo as you have local ammoToUse = math.min(ClipSize - AmmoInClip, SpareAmmo) AmmoInClip = AmmoInClip + ammoToUse SpareAmmo = SpareAmmo - ammoToUse UpdateAmmo(AmmoInClip) end Reloading = false end end
function OnFire() if IsShooting then return end if MyZombie and MyZombie.Health > 0 then if RecoilTrack and AmmoInClip > 0 then RecoilTrack:Play() end IsShooting = true while LeftButtonDown and AmmoInClip > 0 and not Reloading do if Spread and not DecreasedAimLastShot then Spread = math.min(MaxSpread, Spread + AimInaccuracyStepAmount) UpdateCrosshair(Spread) end DecreasedAimLastShot = not DecreasedAimLastShot if Handle:FindFirstChild('FireSound') then Handle.FireSound:Play() end CreateFlash() if MyMouse then local targetPoint = MyMouse.Hit.p local shootDirection = (targetPoint - Handle.Position).unit -- Adjust the shoot direction randomly off by a little bit to account for recoil shootDirection = CFrame.Angles((0.5 - math.random()) * 2 * Spread, (0.5 - math.random()) * 2 * Spread, (0.5 - math.random()) * 2 * Spread) * shootDirection local hitObject, bulletPos = RayCast(Handle.Position, shootDirection, Range) local bullet -- Create a bullet here if hitObject then bullet = CreateBullet(bulletPos) end if hitObject and hitObject.Parent then local hitZombie = hitObject.Parent:FindFirstChild("Zombie") if hitZombie then local hitPlayer = game.Players:GetPlayerFromCharacter(hitZombie.Parent) if MyPlayer.Neutral or (hitPlayer and hitPlayer.TeamColor ~= MyPlayer.TeamColor) then TagZombie(hitZombie, MyPlayer) hitZombie:TakeDamage(Damage) if bullet then bullet:Destroy() bullet = nil --bullet.Transparency = 1 end Spawn(UpdateTargetHit) end end end
AmmoInClip = AmmoInClip - 1 UpdateAmmo(AmmoInClip) end wait(FireRate) end IsShooting = false if AmmoInClip == 0 then Reload() end if RecoilTrack then RecoilTrack:Stop() end end end
local TargetHits = 0 function UpdateTargetHit() TargetHits = TargetHits + 1 if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then WeaponGui.Crosshair.TargetHitImage.Visible = true end wait(0.5) TargetHits = TargetHits - 1 if TargetHits == 0 and WeaponGui and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('TargetHitImage') then WeaponGui.Crosshair.TargetHitImage.Visible = false end end
function UpdateCrosshair(value, mouse) if WeaponGui then local absoluteY = 650 WeaponGui.Crosshair:TweenSize( UDim2.new(0, value * absoluteY * 2 + 23, 0, value * absoluteY * 2 + 23), Enum.EasingDirection.Out, Enum.EasingStyle.Linear, 0.33) end end
function UpdateAmmo(value) if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('ClipAmmo') then WeaponGui.AmmoHud.ClipAmmo.Text = AmmoInClip if value > 0 and WeaponGui:FindFirstChild('Crosshair') and WeaponGui.Crosshair:FindFirstChild('ReloadingLabel') then WeaponGui.Crosshair.ReloadingLabel.Visible = false end end if WeaponGui and WeaponGui:FindFirstChild('AmmoHud') and WeaponGui.AmmoHud:FindFirstChild('TotalAmmo') then WeaponGui.AmmoHud.TotalAmmo.Text = SpareAmmo end end
function OnMouseDown() LeftButtonDown = true OnFire() end
function OnMouseUp() LeftButtonDown = false end
function OnKeyDown(key) if string.lower(key) == 'r' then Reload() end end
function OnEquipped(mouse) RecoilAnim = WaitForChild(Tool, 'Recoil') FireSound = WaitForChild(Handle, 'FireSound')
MyCharacter = Tool.Parent MyPlayer = game:GetService('Players'):GetPlayerFromCharacter(MyCharacter) MyZombie = MyCharacter:FindFirstChild('Zombie') MyTorso = MyCharacter:FindFirstChild('Torso') MyMouse = mouse WeaponGui = WaitForChild(Tool, 'WeaponHud'):Clone() if WeaponGui and MyPlayer then WeaponGui.Parent = MyPlayer.PlayerGui UpdateAmmo(AmmoInClip) end if RecoilAnim then RecoilTrack = MyHumanoid:LoadAnimation(RecoilAnim) end
if MyMouse then -- Disable mouse icon MyMouse.Icon = "http://www.roblox.com/asset/?id=18662154" MyMouse.Button1Down:connect(OnMouseDown) MyMouse.Button1Up:connect(OnMouseUp) MyMouse.KeyDown:connect(OnKeyDown) end end
-- Unequip logic here function OnUnequipped() LeftButtonDown = false Reloading = false MyCharacter = nil MyHumanoid = nil MyTorso = nil MyPlayer = nil MyMouse = nil if OnFireConnection then OnFireConnection:disconnect() end if OnReloadConnection then OnReloadConnection:disconnect() end if FlashHolder then FlashHolder = nil end if WeaponGui then WeaponGui.Parent = nil WeaponGui = nil end if RecoilTrack then RecoilTrack:Stop() end end
local function SetReticleColor(color) if WeaponGui and WeaponGui:FindFirstChild('Crosshair') then for _, line in pairs(WeaponGui.Crosshair:GetChildren()) do if line:IsA('Frame') then line.BorderColor3 = color end end end end
Tool.Equipped:connect(OnEquipped) Tool.Unequipped:connect(OnUnequipped)
while true do wait(0.033) if WeaponGui and WeaponGui:FindFirstChild('Crosshair') and MyMouse then WeaponGui.Crosshair.Position = UDim2.new(0, MyMouse.X, 0, MyMouse.Y) SetReticleColor(NeutralReticleColor)
local target = MyMouse.Target if target and target.Parent then local player = PlayersService:GetPlayerFromCharacter(target.Parent) if player then if MyPlayer.Neutral or player.TeamColor ~= MyPlayer.TeamColor then SetReticleColor(EnemyReticleColor) else SetReticleColor(FriendlyReticleColor) end end end end if Spread and not IsShooting then local currTime = time() if currTime - LastSpreadUpdate > FireRate * 2 then LastSpreadUpdate = currTime Spread = math.max(MinSpread, Spread - AimInaccuracyStepAmount) UpdateCrosshair(Spread, MyMouse) end end end |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:39 PM |
@Inital, Just tried it, I can't shoot the gun at all now,
ravioli ravioli give me the formuoli |
|
|
| Report Abuse |
|
|
| |
|
|
| 16 Mar 2014 07:42 PM |
I was getting this error, 19:41:16.079 - Players.Player1.Backpack.StarterPistol.StarterPistolScript:326: attempt to index global 'MyHumanoid' (a nil value) 19:41:16.080 - Script 'Players.Player1.Backpack.StarterPistol.StarterPistolScript', Line 326
ravioli ravioli give me the formuoli |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:44 PM |
| Push CTRL F and type in the little box "Humanoid" and make it Zombie |
|
|
| Report Abuse |
|
|
|
| 16 Mar 2014 07:54 PM |
Still not shooting
ravioli ravioli give me the formuoli |
|
|
| Report Abuse |
|
|
|
| |
|
|
| |
|
»
»
|
|
|
|
|