Advertisement
Plug_RBLX

long pistol

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