Advertisement
Guest User

pistol roblox

a guest
Apr 19th, 2015
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.52 KB | None | 0 0
  1. --Thanks for using Build2Script Plugin by jarredbcv!
  2. if (script:FindFirstChild("forCustomRun")~=nil) then
  3. local sc = script["forCustomRun"].Value
  4. assert(loadstring(sc))()
  5. return
  6. elseif (script:FindFirstChild("forCustomRunL")~=nil) then
  7. local locsc = workspace:FindFirstChild("_localrun")
  8. local sc = script["forCustomRunL"]
  9. if (locsc~=nil) then
  10. local loc = locsc:clone()
  11. loc["scrun"].Value = sc.Value
  12. loc.Name = script.Name
  13. for i,v in pairs(script:GetChildren()) do
  14. v:clone().Parent = loc
  15. end
  16. loc.Parent = script.Parent
  17. script:remove()
  18. return
  19. else
  20. assert(loadstring(sc.Value))()
  21. return
  22. end
  23. end
  24. local m = Instance.new("Model")
  25. m.Name = "Pistol"
  26. t1 = Instance.new("Tool", m)
  27. t1.Name = "Pistol"
  28. t1.GripForward = Vector3.new(0, 1, 0)
  29. t1.GripPos = Vector3.new(0, -0.400000006, -0.400000006)
  30. t1.GripUp = Vector3.new(0, 0, 1)
  31. x1 = Instance.new("Configuration", t1)
  32. x1.Name = "Configurations"
  33. v1 = Instance.new("IntValue", x1)
  34. v1.Name = "ClipSize"
  35. v1.Value = 8
  36. v2 = Instance.new("NumberValue", x1)
  37. v2.Name = "AttackCooldown"
  38. v2.Value = 0.15
  39. v3 = Instance.new("NumberValue", x1)
  40. v3.Name = "Range"
  41. v3.Value = 300
  42. v4 = Instance.new("IntValue", x1)
  43. v4.Name = "Damage"
  44. v4.Value = 20
  45. v5 = Instance.new("NumberValue", x1)
  46. v5.Name = "ReloadTime"
  47. v5.Value = 1
  48. p1 = Instance.new("Part", t1)
  49. p1.BrickColor = BrickColor.new("Dark stone grey")
  50. p1.Name = "Handle"
  51. p1.FormFactor = Enum.FormFactor.Custom
  52. p1.Size = Vector3.new(0.200000003, 0.800000012, 0.800000012)
  53. p1.CFrame = CFrame.new(16.9000015, 0.399999291, -16.4000015, 1, 1.24442863e-07, 1.96370365e-06, -1.96370365e-06, -2.29479701e-06, 1, 1.24447368e-07, -1, -2.29479679e-06)
  54. p1.BackSurface = Enum.SurfaceType.Weld
  55. p1.BottomSurface = Enum.SurfaceType.Weld
  56. p1.FrontSurface = Enum.SurfaceType.Weld
  57. p1.LeftSurface = Enum.SurfaceType.Weld
  58. p1.RightSurface = Enum.SurfaceType.Weld
  59. p1.TopSurface = Enum.SurfaceType.Weld
  60. b1 = Instance.new("SpecialMesh", p1)
  61. b1.MeshId = "http://www.roblox.com/asset/?id=72012879"
  62. b1.TextureId = "http://www.roblox.com/asset/?id=72012859"
  63. b1.MeshType = Enum.MeshType.FileMesh
  64. b1.Name = "Mesh"
  65. b1.VertexColor = Vector3.new(2, 2, 2)
  66. b1.Scale = Vector3.new(1.25, 1.25, 1.25)
  67. s1 = script:clone()
  68. s1.Name = "LocalScript"
  69. s1.Disabled = false
  70. so1 = Instance.new("StringValue", s1)
  71. so1.Name = "forCustomRunL"
  72. so1.Value = [===[
  73. -- Variables for services
  74. local render = game:GetService("RunService").RenderStepped
  75. local contextActionService = game:GetService("ContextActionService")
  76. local userInputService = game:GetService("UserInputService")
  77.  
  78. local player = game.Players.LocalPlayer
  79. local mouse = player:GetMouse()
  80. local Tool = script.Parent
  81.  
  82. -- Variables for Module Scripts
  83. local screenSpace = require(Tool:WaitForChild("ScreenSpace"))
  84.  
  85. local connection
  86. -- Variables for character joints
  87.  
  88. local neck, shoulder, oldNeckC0, oldShoulderC0
  89.  
  90. local mobileShouldTrack = true
  91.  
  92. -- Thourough check to see if a character is sitting
  93. local function amISitting(character)
  94. local t = character.Torso
  95. for _, part in pairs(t:GetConnectedParts(true)) do
  96. if part:IsA("Seat") or part:IsA("VehicleSeat") then
  97. return true
  98. end
  99. end
  100. end
  101.  
  102. -- Function to call on renderstepped. Orients the character so it is facing towards
  103. -- the player mouse's position in world space. If character is sitting then the torso
  104. -- should not track
  105. local function frame(mousePosition)
  106. -- Special mobile consideration. We don't want to track if the user was touching a ui
  107. -- element such as the movement controls. Just return out of function if so to make sure
  108. -- character doesn't track
  109. if not mobileShouldTrack then return end
  110.  
  111. -- Make sure character isn't swiming. If the character is swimming the following code will
  112. -- not work well; the character will not swim correctly. Besides, who shoots underwater?
  113. if player.Character.Humanoid:GetState() ~= Enum.HumanoidStateType.Swimming then
  114. local torso = player.Character.Torso
  115. local head = player.Character.Head
  116.  
  117. local toMouse = (mousePosition - head.Position).unit
  118. local angle = math.acos(toMouse:Dot(Vector3.new(0,1,0)))
  119.  
  120. local neckAngle = angle
  121.  
  122. -- Limit how much the head can tilt down. Too far and the head looks unnatural
  123. if math.deg(neckAngle) > 110 then
  124. neckAngle = math.rad(110)
  125. end
  126. neck.C0 = CFrame.new(0,1,0) * CFrame.Angles(math.pi - neckAngle,math.pi,0)
  127.  
  128. -- Calculate horizontal rotation
  129. local arm = player.Character:FindFirstChild("Right Arm")
  130. local fromArmPos = torso.Position + torso.CFrame:vectorToWorldSpace(Vector3.new(
  131. torso.Size.X/2 + arm.Size.X/2, torso.Size.Y/2 - arm.Size.Z/2, 0))
  132. local toMouseArm = ((mousePosition - fromArmPos) * Vector3.new(1,0,1)).unit
  133. local look = (torso.CFrame.lookVector * Vector3.new(1,0,1)).unit
  134. local lateralAngle = math.acos(toMouseArm:Dot(look))
  135.  
  136. -- Check for rogue math
  137. if tostring(lateralAngle) == "-1.#IND" then
  138. lateralAngle = 0
  139. end
  140.  
  141. -- Handle case where character is sitting down
  142. if player.Character.Humanoid:GetState() == Enum.HumanoidStateType.Seated then
  143.  
  144. local cross = torso.CFrame.lookVector:Cross(toMouseArm)
  145. if lateralAngle > math.pi/2 then
  146. lateralAngle = math.pi/2
  147. end
  148. if cross.Y < 0 then
  149. lateralAngle = -lateralAngle
  150. end
  151. end
  152.  
  153. -- Turn shoulder to point to mouse
  154. shoulder.C0 = CFrame.new(1,0.5,0) * CFrame.Angles(math.pi/2 - angle,math.pi/2 + lateralAngle,0)
  155.  
  156. -- If not sitting then aim torso laterally towards mouse
  157. if not amISitting(player.Character) then
  158. torso.CFrame = CFrame.new(torso.Position, torso.Position + (Vector3.new(
  159. mousePosition.X, torso.Position.Y, mousePosition.Z)-torso.Position).unit)
  160. else
  161. --print("sitting")
  162. end
  163. end
  164. end
  165.  
  166. -- Function to bind to render stepped if player is on PC
  167. local function pcFrame()
  168. frame(mouse.Hit.p)
  169. end
  170.  
  171. -- Function to bind to touch moved if player is on mobile
  172. local function mobileFrame(touch, processed)
  173. -- Check to see if the touch was on a UI element. If so, we don't want to update anything
  174. if not processed then
  175. -- Calculate touch position in world space. Uses Stravant's ScreenSpace Module script
  176. -- to create a ray from the camera.
  177. local test = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1)
  178. local nearPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1))
  179. nearPos = game.Workspace.CurrentCamera.CoordinateFrame.p - nearPos
  180. local farPos = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y,50)
  181. farPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(farPos) * -1
  182. if farPos.magnitude > 900 then
  183. farPos = farPos.unit * 900
  184. end
  185. local ray = Ray.new(nearPos, farPos)
  186. local part, pos = game.Workspace:FindPartOnRay(ray, player.Character)
  187.  
  188. -- if a position was found on the ray then update the character's rotation
  189. if pos then
  190. frame(pos)
  191. end
  192. end
  193. end
  194.  
  195. local oldIcon = nil
  196. -- Function to bind to equip event
  197. local function equip()
  198. local torso = player.Character.Torso
  199.  
  200. -- Setup joint variables
  201. neck = torso.Neck
  202. oldNeckC0 = neck.C0
  203. shoulder = torso:FindFirstChild("Right Shoulder")
  204. oldShoulderC0 = shoulder.C0
  205.  
  206. -- Remember old mouse icon and update current
  207. oldIcon = mouse.Icon
  208. mouse.Icon = "http://www.roblox.com/asset/?id=79658449"
  209.  
  210. -- Bind TouchMoved event if on mobile. Otherwise connect to renderstepped
  211. if userInputService.TouchEnabled then
  212. connection = userInputService.TouchMoved:connect(mobileFrame)
  213. else
  214. connection = render:connect(pcFrame)
  215. end
  216.  
  217. -- Bind TouchStarted and TouchEnded. Used to determine if character should rotate
  218. -- during touch input
  219. userInputService.TouchStarted:connect(function(touch, processed)
  220. mobileShouldTrack = not processed
  221. end)
  222. userInputService.TouchEnded:connect(function(touch, processed)
  223. mobileShouldTrack = false
  224. end)
  225.  
  226. -- Fire server's equip event
  227. game.ReplicatedStorage.ROBLOX_PistolEquipEvent:FireServer()
  228.  
  229. -- Bind event for when mouse is clicked to fire server's fire event
  230. mouse.Button1Down:connect(function()
  231. game.ReplicatedStorage.ROBLOX_PistolFireEvent:FireServer(mouse.Hit.p)
  232. end)
  233.  
  234. -- Bind reload event to mobile button and r key
  235. contextActionService:BindActionToInputTypes("Reload", function()
  236. game.ReplicatedStorage.ROBLOX_PistolReloadEvent:FireServer()
  237. end, true, "r")
  238.  
  239. -- If game uses filtering enabled then need to update server while tool is
  240. -- held by character.
  241. if workspace.FilteringEnabled then
  242. while connection do
  243. wait()
  244. game.ReplicatedStorage.ROBLOX_PistolUpdateEvent:FireServer(neck.C0, shoulder.C0)
  245. end
  246. end
  247. end
  248.  
  249. -- Function to bind to Unequip event
  250. local function unequip()
  251. if connection then connection:disconnect() end
  252. contextActionService:UnbindAction("Reload")
  253. game.ReplicatedStorage.ROBLOX_PistolUnequipEvent:FireServer()
  254. mouse.Icon = oldIcon
  255. neck.C0 = oldNeckC0
  256. shoulder.C0 = oldShoulderC0
  257. end
  258.  
  259. -- Bind tool events
  260. Tool.Equipped:connect(equip)
  261. Tool.Unequipped:connect(unequip)
  262. ]===]
  263. s1.Parent = t1
  264. s2 = script:clone()
  265. s2.Name = "Script"
  266. s2.Disabled = false
  267. so2 = Instance.new("StringValue", s2)
  268. so2.Name = "forCustomRun"
  269. so2.Value = [===[
  270. -- Local variables
  271. local tool = script.Parent
  272. local currentAmmo = tool.Configurations.ClipSize.Value
  273. local canFire = true
  274. local reloading = false
  275. local fireSound = tool.FireSound
  276.  
  277. -- Configurable variables
  278. local attackCooldown = tool.Configurations.AttackCooldown.Value
  279. local range = tool.Configurations.Range.Value
  280. local damage = tool.Configurations.Damage.Value
  281. local reloadTime = tool.Configurations.ReloadTime.Value
  282. local clipSize = tool.Configurations.ClipSize.Value
  283.  
  284. -- Setup Remote Events
  285. local function createEvent(eventName)
  286. local event = game.ReplicatedStorage:FindFirstChild(eventName)
  287. if not event then
  288. event = Instance.new("RemoteEvent", game.ReplicatedStorage)
  289. event.Name = eventName
  290. end
  291. return event
  292. end
  293. local updateEvent = createEvent("ROBLOX_PistolUpdateEvent")
  294. local equipEvent = createEvent("ROBLOX_PistolEquipEvent")
  295. local unequipEvent = createEvent("ROBLOX_PistolUnequipEvent")
  296. local fireEvent = createEvent("ROBLOX_PistolFireEvent")
  297. local reloadEvent = createEvent("ROBLOX_PistolReloadEvent")
  298.  
  299. -- Add tracer decal to server storage if it isn't already there
  300. if not game.ServerStorage:FindFirstChild("ROBLOX_PistolTracerDecal") then
  301. tool.ROBLOX_PistolTracerDecal:Clone().Parent = game.ServerStorage
  302. end
  303.  
  304. -- Bind function to update event. Used to update player's orientation if FilteringEnabled
  305. -- is true (otherwise the rotation would not replicate from the rotating player)
  306. updateEvent.OnServerEvent:connect(function(player, neckC0, rshoulderC0)
  307. local character = player.Character
  308. character.Torso.Neck.C0 = neckC0
  309. character.Torso:FindFirstChild("Right Shoulder").C0 = rshoulderC0
  310. end)
  311.  
  312. -- Bind functions to when player equips/unequips the tool. Right now just need to turn on and
  313. -- off AutoRotate
  314. equipEvent.OnServerEvent:connect(function(player)
  315. player.Character.Humanoid.AutoRotate = false
  316. end)
  317. unequipEvent.OnServerEvent:connect(function(player)
  318. player.Character.Humanoid.AutoRotate = true
  319. end)
  320.  
  321. -- Creates "bullet". No projectile motion is actually used. Pistol raytraces to target and creates
  322. -- a tracer trail to the target. Fading trail gives illusion of motion.
  323. local function createBullet(target)
  324. -- Get actual handle position. Want to offset from the center of the handle as the bullet comes
  325. -- from the barrel of the gun
  326. local handlePos = tool.Handle.CFrame + tool.Handle.CFrame:vectorToWorldSpace(Vector3.new(0,0,.3))
  327. local toTarget = handlePos:vectorToWorldSpace(Vector3.new(0,1,0)) * 200
  328. local torsoLook = (tool.Parent:FindFirstChild("Torso").CFrame.lookVector * Vector3.new(1,0,1)).unit
  329. local toTargetAngle = (toTarget * Vector3.new(1,0,1)).unit
  330. local angle = math.acos(torsoLook:Dot(toTargetAngle))
  331.  
  332. -- Checks angle from where the character is facing to the orientation of the pistol. If the angle
  333. -- is less than 90 degress then we shoot to where the mouse is pointing (helps accuracy). Otherwise
  334. -- the gun is assumed at the edge of its rotation and just shoots straight.
  335. if math.deg(angle) < 90 then
  336. toTarget = target - tool.Handle.Position
  337. if toTarget.magnitude > range then
  338. toTarget = toTarget.unit * range
  339. end
  340. toTarget = toTarget * 1.1
  341. end
  342.  
  343. -- Shoot ray and check if humanoid was hit. If so, it should take damage
  344. local ray = Ray.new(handlePos.p, toTarget)
  345. local part, position = game.Workspace:FindPartOnRay(ray, tool.Parent)
  346. if part and part.Parent and part.Parent:FindFirstChild("Humanoid") then
  347. part.Parent:FindFirstChild("Humanoid"):TakeDamage(damage)
  348. end
  349.  
  350. if position then
  351. toTarget = position - handlePos.p
  352. end
  353.  
  354. -- Create tracer trail. Trail is made of thin parts 2 studs long. Fades each segment
  355. -- starting with closest tracer to the tool.
  356. local bulletTrail = Instance.new("Model", game.Workspace)
  357. local trailTable = {}
  358. -- Fetch decal from server storage
  359. local decal = game.ServerStorage.ROBLOX_PistolTracerDecal
  360. for i = 0, toTarget.magnitude/2, 1 do
  361. local trailSegment = Instance.new("Part", bulletTrail)
  362. trailSegment.CanCollide = false
  363. trailSegment.Anchored = true
  364. trailSegment.FormFactor = Enum.FormFactor.Custom
  365. trailSegment.Size = Vector3.new(.1,.1,2)
  366. trailSegment.BrickColor = BrickColor.White()
  367. trailSegment.CFrame = CFrame.new(handlePos.p + (toTarget.unit * 2 * (i + .5)), handlePos.p)
  368. trailSegment.Transparency = 1
  369.  
  370. -- Add point light to tracer for a little illumination
  371. local light = Instance.new("PointLight", trailSegment)
  372. light.Range = 3
  373.  
  374. -- Add decal to faces of the part
  375. local function addDecal(face)
  376. local decalClone = decal:Clone()
  377. decalClone.Parent = trailSegment
  378. decalClone.Face = face
  379. end
  380.  
  381. addDecal(Enum.NormalId.Top)
  382. addDecal(Enum.NormalId.Bottom)
  383. addDecal(Enum.NormalId.Left)
  384. addDecal(Enum.NormalId.Right)
  385.  
  386. -- Add segment to all of the tracers
  387. table.insert(trailTable, trailSegment)
  388. end
  389.  
  390. -- Coroutine thread to fade each trail segment. Put in coroutine so it does not
  391. -- block the rest of the pistol's script
  392. local fadeThread = coroutine.create(function()
  393. local count = 1
  394. local ended = false
  395. -- Keep looping until end condition is met
  396. while not ended do
  397. -- Assume end condition is met. Easier to switch it off later if we need to
  398. -- keep looping
  399. ended = true
  400. -- Loop through every part in the trail
  401. for index, part in pairs(trailTable) do
  402. if index <= count then
  403. local shouldDestroy = false
  404. for _, face in pairs(part:GetChildren()) do
  405.  
  406. if face:IsA("Decal") then
  407. -- Increase decal transparencies and use this to determine if
  408. -- segment has completely faded
  409. face.Transparency = face.Transparency + .05
  410. if face.Transparency < 1 then
  411. ended = false
  412. else
  413. shouldDestroy = true
  414. end
  415. else
  416. -- Dim the point light
  417. face.Brightness = face.Brightness - .1
  418. end
  419. end
  420. -- If segment is completely faded then clean it up
  421. if shouldDestroy then
  422. table.remove(trailTable, index)
  423. part:Destroy()
  424. end
  425. end
  426. end
  427. count = count + 1
  428. wait()
  429. end
  430. bulletTrail:Destroy()
  431. end)
  432. coroutine.resume(fadeThread)
  433. end
  434.  
  435. -- Function to bind to reload event
  436. local function reload()
  437. if not reloading then
  438. tool.ReloadSound:Play()
  439. reloading = true
  440. canFire = false
  441. wait(reloadTime)
  442. currentAmmo = clipSize
  443. canFire = true
  444. reloading = false
  445. end
  446. end
  447.  
  448. reloadEvent.OnServerEvent:connect(reload)
  449.  
  450. -- Bind function to fire event
  451. fireEvent.OnServerEvent:connect(function(player, target)
  452. if tool.Parent == player.Character then
  453. -- If tool has enough shots then fires. Otherwise reloads.
  454. if currentAmmo <= 0 then
  455. return reload()
  456. end
  457. if canFire then
  458. canFire = false
  459. currentAmmo = currentAmmo - 1
  460. fireSound:Play()
  461. createBullet(target)
  462. delay(attackCooldown, function()
  463. canFire = true
  464. end)
  465. end
  466. end
  467. end)
  468.  
  469.  
  470. ]===]
  471. s2.Parent = t1
  472. x2 = Instance.new("Sound", t1)
  473. x2.Name = "ReloadSound"
  474. x2.Pitch = 0.69999998807907
  475. x2.SoundId = "http://www.roblox.com/asset/?id=31762599"
  476. x2.Volume = 1
  477. x2.Looped = false
  478. x2.PlayOnRemove = false
  479. x3 = Instance.new("Sound", t1)
  480. x3.Name = "FireSound"
  481. x3.Pitch = 0.69999998807907
  482. x3.SoundId = "http://www.roblox.com/asset/?id=12221976"
  483. x3.Volume = 0.5
  484. x3.Looped = false
  485. x3.PlayOnRemove = false
  486. d1 = Instance.new("Decal", t1)
  487. d1.Texture = "http://www.roblox.com/asset/?id=186982304"
  488. d1.Transparency = 0.5
  489. d1.Face = Enum.NormalId.Top
  490. m.Parent = game:service("Workspace")
  491. m:MakeJoints()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement