Gun script for roblox - Pastebin.com (2024)

  1. -------------------------------------Gun info

  2. ToolName="Assault Rifle"

  3. ClipSize=30

  4. ReloadTime=2

  5. Firerate=.08

  6. MinSpread=0.15

  7. MaxSpread=0.15

  8. SpreadRate=0.2

  9. BaseDamage= 15

  10. automatic=true

  11. burst=false

  12. shot=false--Shotgun

  13. BarrlePos=Vector3.new(-2.5,.60,0)

  14. Cursors={"rbxasset://textures\\GunCursor.png"}

  15. ReloadCursor="rbxasset://textures\\GunWaitCursor.png"

  16. -------------------------------------

  17. equiped=false

  18. sp=script.Parent

  19. RayLength=1000

  20. Spread=0.15

  21. enabled=true

  22. reloading=false

  23. down=false

  24. r=game:service("RunService")

  25. last=0

  26. last2=0

  27. last3=0

  28. last4=0

  29. last5=0

  30. last6=0

  31. Bullet=Instance.new("Part")

  32. Bullet.Name="Bullet"

  33. Bullet.BrickColor=BrickColor.new("New Yeller")

  34. Bullet.Anchored=true

  35. Bullet.CanCollide=false

  36. Bullet.Locked=true

  37. Bullet.Size=Vector3.new(1,1,1)

  38. --Bullet.Transparency=.65

  39. Bullet.formFactor=0

  40. Bullet.TopSurface=0

  41. Bullet.BottomSurface=0

  42. mesh=Instance.new("SpecialMesh")

  43. mesh.Parent=Bullet

  44. mesh.MeshType="Brick"

  45. mesh.Name="Mesh"

  46. mesh.Scale=Vector3.new(.15,.15,1)

  47. function check()

  48. sp.Name=ToolName.." ["..tostring(sp.Ammo.Value).."]"

  49. end

  50. function computeDirection(vec)

  51. local lenSquared = vec.magnitude * vec.magnitude

  52. local invSqrt = 1 / math.sqrt(lenSquared)

  53. return Vector3.new(vec.x * invSqrt, vec.y * invSqrt, vec.z * invSqrt)

  54. end

  55. ------------------------------------------------------------------------------------Raycasting functions

  56. function cross(vector1, vector2)

  57. return Vector3.new(vector1.y * vector2.z - vector2.y * vector1.z, vector1.z * vector2.x - vector1.x * vector2.z, vector1.x * vector2.y - vector2.x * vector1.y)

  58. end

  59. function dot(vector1, vector2)

  60. return (vector1.x * vector2.x + vector1.y * vector2.y + vector1.z * vector2.z)

  61. end

  62. function getLineSphereCollide(linePoint1, lineVector, sphereCenter, radius)

  63. local a = lineVector.x * lineVector.x + lineVector.y * lineVector.y + lineVector.z * lineVector.z

  64. local b = lineVector.x * (linePoint1.x - sphereCenter.x) + lineVector.y * (linePoint1.y - sphereCenter.y) + lineVector.z * (linePoint1.z - sphereCenter.z)

  65. local c = (linePoint1.x - sphereCenter.x) * (linePoint1.x - sphereCenter.x) + (linePoint1.y - sphereCenter.y) * (linePoint1.y - sphereCenter.y) + (linePoint1.z - sphereCenter.z) * (linePoint1.z - sphereCenter.z) - radius * radius

  66. if (a > 0) and (b * b >= a * c) then

  67. local diff = math.sqrt(b * b - a * c)

  68. return ((-b - diff) / a), ((diff - b) / a)

  69. else

  70. return -1, -1

  71. end

  72. end

  73. --Returns hit, position, normal, time

  74. function raycast(model, start, vector, brickFunction)

  75. local hit, normal, time = raycastRecursive(model, start, vector, brickFunction, vector.unit, dot(start, vector.unit))

  76. if (dot(normal, vector) > 0) then

  77. normal = -normal

  78. end

  79. return hit, start + time * vector, normal.unit, time

  80. end

  81. function raycastRecursive(model, start, vector, brickFunction, unitVec, startDist)

  82. if (model.className == "Part") or (model.className == "Seat") or (model.className =="SpawnLocation") then

  83. local range = model.Size.magnitude / 2

  84. local dist = dot(model.Position, unitVec) - startDist

  85. if (dist + range > 0) and (dist - range < vector.magnitude) and ((dist * unitVec + start - model.Position).magnitude < range) and brickFunction(model) then

  86. local halfSize = model.Size / 2

  87. if (model.Shape == Enum.PartType.Ball) then

  88. local time, timeMax = getLineSphereCollide(start, vector, model.Position, halfSize.x)

  89. if (time < 1) and (time >= 0) then

  90. return model, (time * vector + start - model.Position), time

  91. else

  92. return nil, Vector3.new(0, 0, 0), 1

  93. end

  94. elseif (model.Shape == Enum.PartType.Block) then

  95. local time = 1

  96. local cf = model.CFrame - model.Position

  97. local xvec = cf * Vector3.new(1, 0, 0)

  98. local yvec = cf * Vector3.new(0, 1, 0)

  99. local zvec = cf * Vector3.new(0, 0, 1)

  100. local xspd = -dot(xvec, vector)

  101. local yspd = -dot(yvec, vector)

  102. local zspd = -dot(zvec, vector)

  103. local xmin, xmax, ymin, ymax, zmin, zmax = -1

  104. local dotProd = dot(xvec, start - model.Position)

  105. if (xspd ~= 0) then

  106. xmin = (dotProd - halfSize.x) / xspd

  107. xmax = (dotProd + halfSize.x) / xspd

  108. if (xmax < xmin) then

  109. local swap = xmin

  110. xmin = xmax

  111. xmax = swap

  112. end

  113. else

  114. if (math.abs(dotProd) < halfSize.x) then

  115. xmax = 1

  116. xmin = 0

  117. else

  118. return nil, Vector3.new(0, 0, 0), 1

  119. end

  120. end

  121. local dotProd = dot(yvec, start - model.Position)

  122. if (yspd ~= 0) then

  123. ymin = (dotProd - halfSize.y) / yspd

  124. ymax = (dotProd + halfSize.y) / yspd

  125. if (ymax < ymin) then

  126. local swap = ymin

  127. ymin = ymax

  128. ymax = swap

  129. end

  130. else

  131. if (math.abs(dotProd) < halfSize.y) then

  132. ymax = 1

  133. ymin = 0

  134. else

  135. return nil, Vector3.new(0, 0, 0), 1

  136. end

  137. end

  138. local dotProd = dot(zvec, start - model.Position)

  139. if (zspd ~= 0) then

  140. zmin = (dotProd - halfSize.z) / zspd

  141. zmax = (dotProd + halfSize.z) / zspd

  142. if (zmax < zmin) then

  143. local swap = zmin

  144. zmin = zmax

  145. zmax = swap

  146. end

  147. else

  148. if (math.abs(dotProd) < halfSize.z) then

  149. zmax = 1

  150. zmin = 0

  151. else

  152. return nil, Vector3.new(0, 0, 0), 1

  153. end

  154. end

  155. if (xmin <= ymax) and (xmax >= ymin) and (xmin <= zmax) and (xmax >= zmin) and (zmin <= ymax) and (zmax >= ymin) then

  156. local normal = xvec

  157. local min = xmin

  158. if (ymin > min) then

  159. min = ymin

  160. normal = yvec

  161. end

  162. if (zmin > min) then

  163. min = zmin

  164. normal = zvec

  165. end

  166. if (min >= 0) and (min < 1) then

  167. time = min

  168. elseif (xmax > 0) and (ymax > 0) and (zmax > 0) and (min < 0) then

  169. time = 0

  170. normal = Vector3.new(0, 0, 0)

  171. end

  172. return model, normal, time

  173. else

  174. return nil, Vector3.new(0, 0, 0), 1

  175. end

  176. else--Cylinder

  177. local time = 1

  178. local cf = model.CFrame - model.Position

  179. local xvec = cf * Vector3.new(1, 0, 0)

  180. local xspd = -dot(xvec, vector)

  181. local xmin, xmax = -1

  182. local dotProd = dot(xvec, start - model.Position)

  183. if (xspd ~= 0) then

  184. xmin = (dotProd - halfSize.x) / xspd

  185. xmax = (dotProd + halfSize.x) / xspd

  186. if (xmax < xmin) then

  187. local swap = xmin

  188. xmin = xmax

  189. xmax = swap

  190. end

  191. else

  192. if (math.abs(dotProd) < halfSize.x) then

  193. xmax = 1

  194. xmin = 0

  195. else

  196. return nil, Vector3.new(0, 0, 0), 1

  197. end

  198. end

  199. local relVec = cf:pointToObjectSpace(vector) * Vector3.new(0, 1, 1)

  200. local relPos = model.CFrame:pointToObjectSpace(start) * Vector3.new(0, 1, 1)

  201. local rmin, rmax = getLineSphereCollide(relPos, relVec, Vector3.new(0, 0, 0), halfSize.y)

  202. if (xmin <= rmax) and (xmax >= rmin) and (rmax > 0) then

  203. local normal = xvec

  204. local min = xmin

  205. if (rmin > min) then

  206. min = rmin

  207. normal = cf * (relPos + relVec * min)

  208. end

  209. if (min >= 0) and (min < 1) then

  210. time = min

  211. elseif (xmax > 0) and (rmax > 0) and (min < 0) then

  212. time = 0

  213. normal = Vector3.new(0, 0, 0)

  214. end

  215. return model, normal, time

  216. else

  217. return nil, Vector3.new(0, 0, 0), 1

  218. end

  219. return nil, Vector3.new(0, 0, 0), 1

  220. end

  221. end

  222. return nil, Vector3.new(0, 0, 0), 1

  223. elseif (model.className=="Model") or (model.className=="Workspace") or (model.className=="Hat") or (model.className == "Tool") then

  224. local children=model:GetChildren()

  225. local time=1

  226. local normal=Vector3.new(0, 0, 0)

  227. local hit=nil

  228. for n = 1, #children do

  229. if children[n]~= nil then

  230. local newHit, newNormal, newTime = raycastRecursive(children[n], start, vector, brickFunction, unitVec, startDist)

  231. if (newTime < time) then

  232. time = newTime

  233. hit = newHit

  234. normal = newNormal

  235. end

  236. end

  237. end

  238. return hit, normal, time

  239. else

  240. return nil, Vector3.new(0, 0, 0), 1

  241. end

  242. end

  243. -------------------------------------------------------------------------------

  244. function tagHumanoid(humanoid)

  245. local plr=game.Players:playerFromCharacter(sp.Parent)

  246. if plr~=nil then

  247. local tag=Instance.new("ObjectValue")

  248. tag.Value=plr

  249. tag.Name="creator"

  250. tag.Parent=humanoid

  251. delay(2,function()

  252. if tag~=nil then

  253. tag.Parent=nil

  254. end

  255. end)

  256. end

  257. end

  258. function reload(mouse)

  259. reloading=true

  260. mouse.Icon=ReloadCursor

  261. while sp.Ammo.Value<ClipSize and reloading and enabled do

  262. wait(ReloadTime/ClipSize)

  263. if reloading then

  264. sp.Ammo.Value=sp.Ammo.Value+1

  265. check()

  266. else

  267. break

  268. end

  269. end

  270. check()

  271. mouse.Icon=Cursors[1]

  272. reloading=false

  273. end

  274. function onKeyDown(key,mouse)

  275. key=key:lower()

  276. if key=="r" and not reloading then

  277. reload(mouse)

  278. end

  279. end

  280. function movecframe(p,pos)

  281. p.Parent=game.Lighting

  282. p.Position=pos

  283. p.Parent=game.Workspace

  284. end

  285. function fire(aim)

  286. sp.Handle.Fire:Play()

  287. t=r.Stepped:wait()

  288. last6=last5

  289. last5=last4

  290. last4=last3

  291. last3=last2

  292. last2=last

  293. last=t

  294. local bullet=Bullet:clone()

  295. local totalDist=0

  296. Lengthdist=-RayLength/.5

  297. local startpoint=sp.Handle.CFrame*BarrlePos

  298. local dir=(aim)-startpoint

  299. dir=computeDirection(dir)

  300. local cfrm=CFrame.new(startpoint, dir+startpoint)

  301. local hit,pos,normal,time=raycast(game.Workspace, startpoint, cfrm*Vector3.new(0,0,Lengthdist)-startpoint, function(brick)

  302. if brick.Name=="Glass" then

  303. return true

  304. elseif brick.Name=="Bullet" or brick.Name=="BulletTexture" then

  305. return false

  306. elseif brick:IsDescendantOf(sp.Parent) then

  307. return false

  308. elseif brick.Name=="Handle" then

  309. if brick.Parent:IsDescendantOf(sp.Parent) then

  310. return false

  311. else

  312. return true

  313. end

  314. end

  315. return true

  316. end)

  317. bullet.Parent=game.Workspace

  318. if hit~=nil then

  319. local humanoid=hit.Parent:FindFirstChild("Humanoid")

  320. if humanoid~=nil then

  321. local damage=math.random(BaseDamage-(BaseDamage*.25),BaseDamage+(BaseDamage*.25))

  322. if hit.Name=="Head" then

  323. damage=damage*1.3

  324. elseif hit.Name=="Torso" then

  325. else

  326. damage=damage*.75

  327. end

  328. if humanoid.Health>0 then

  329. local eplr=game.Players:playerFromCharacter(humanoid.Parent)

  330. local plr=game.Players:playerFromCharacter(sp.Parent)

  331. if eplr~=nil and plr~=nil then

  332. --if eplr.TeamColor~=plr.TeamColor or eplr.Neutral or plr.Neutral then

  333. tagHumanoid(humanoid)

  334. humanoid:TakeDamage(damage)

  335. --end

  336. else

  337. tagHumanoid(humanoid)

  338. humanoid:TakeDamage(damage)

  339. end

  340. end

  341. elseif humanoid==nil and hit.Parent:IsA("Hat") then

  342. if hit.Parent.Parent:FindFirstChild("Humanoid") ~= nil then

  343. humanoid = hit.Parent.Parent:FindFirstChild("Humanoid")

  344. local damage=math.random(BaseDamage-(BaseDamage*.25),BaseDamage+(BaseDamage*.25))

  345. if hit.Name=="Head" then

  346. damage=damage*2

  347. elseif hit.Name=="Torso" then

  348. else

  349. damage=damage*.75

  350. end

  351. if humanoid.Health>0 then

  352. local eplr=game.Players:playerFromCharacter(humanoid.Parent)

  353. local plr=game.Players:playerFromCharacter(sp.Parent)

  354. if eplr~=nil and plr~=nil then

  355. --if eplr.TeamColor~=plr.TeamColor or eplr.Neutral or plr.Neutral then

  356. tagHumanoid(humanoid)

  357. humanoid:TakeDamage(damage)

  358. --end

  359. else

  360. tagHumanoid(humanoid)

  361. humanoid:TakeDamage(damage)

  362. end

  363. end

  364. end

  365. end

  366. if (hit.Name == "Part10") or (hit.Name == "Part11") or (hit.Name == "Part21") or (hit.Name == "Part23") or (hit.Name == "Part24") or (hit.Name == "Part8") then

  367. rand = math.random(1,5)

  368. if rand == 3 then

  369. workspace.GlassSound:play()

  370. hit:breakJoints()

  371. end

  372. end

  373. if (hit.Parent:findFirstChild("Hit")) then

  374. hit.Parent.Health.Value = hit.Parent.Health.Value - BaseDamage/3

  375. end

  376. distance=(startpoint-pos).magnitude

  377. bullet.CFrame=cfrm*CFrame.new(0,0,-distance/2)

  378. bullet.Mesh.Scale=Vector3.new(.15,.15,distance)

  379. else

  380. bullet.CFrame=cfrm*CFrame.new(0,0,-RayLength/2)

  381. bullet.Mesh.Scale=Vector3.new(.15,.15,RayLength)

  382. end

  383. if pos~=nil then

  384. end

  385. local deb=game:FindFirstChild("Debris")

  386. if deb==nil then

  387. local debris=Instance.new("Debris")

  388. debris.Parent=game

  389. end

  390. check()

  391. game.Debris:AddItem(bullet,.05)

  392. end

  393. function onButton1Up(mouse)

  394. down=false

  395. end

  396. function onButton1Down(mouse)

  397. h=sp.Parent:FindFirstChild("Humanoid")

  398. if not enabled or reloading or down or h==nil then

  399. return

  400. end

  401. if sp.Ammo.Value>0 and h.Health>0 then

  402. --[[if sp.Ammo.Value<=0 then

  403. if not reloading then

  404. reload(mouse)

  405. end

  406. return

  407. end]]

  408. down=true

  409. enabled=false

  410. while down do

  411. if sp.Ammo.Value<=0 then

  412. break

  413. end

  414. if burst then

  415. local startpoint=sp.Handle.CFrame*BarrlePos

  416. local mag=(mouse.Hit.p-startpoint).magnitude

  417. local rndm=Vector3.new(math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag))

  418. fire(mouse.Hit.p+rndm)

  419. sp.Ammo.Value=sp.Ammo.Value-1

  420. if sp.Ammo.Value<=0 then

  421. break

  422. end

  423. wait(.05)

  424. local startpoint=sp.Handle.CFrame*BarrlePos

  425. local mag2=((mouse.Hit.p+rndm)-startpoint).magnitude

  426. local rndm2=Vector3.new(math.random(-(.1/10)*mag2,(.1/10)*mag2),math.random(-(.1/10)*mag2,(.1/10)*mag2),math.random(-(.1/10)*mag2,(.1/10)*mag2))

  427. fire(mouse.Hit.p+rndm+rndm2)

  428. sp.Ammo.Value=sp.Ammo.Value-1

  429. if sp.Ammo.Value<=0 then

  430. break

  431. end

  432. wait(.05)

  433. fire(mouse.Hit.p+rndm+rndm2+rndm2)

  434. sp.Ammo.Value=sp.Ammo.Value-1

  435. elseif shot then

  436. sp.Ammo.Value=sp.Ammo.Value-1

  437. local startpoint=sp.Handle.CFrame*BarrlePos

  438. local mag=(mouse.Hit.p-startpoint).magnitude

  439. local rndm=Vector3.new(math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag))

  440. fire(mouse.Hit.p+rndm)

  441. local mag2=((mouse.Hit.p+rndm)-startpoint).magnitude

  442. local rndm2=Vector3.new(math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2))

  443. fire(mouse.Hit.p+rndm+rndm2)

  444. local rndm3=Vector3.new(math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2))

  445. fire(mouse.Hit.p+rndm+rndm3)

  446. local rndm4=Vector3.new(math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2),math.random(-(.2/10)*mag2,(.2/10)*mag2))

  447. fire(mouse.Hit.p+rndm+rndm4)

  448. else

  449. sp.Ammo.Value=sp.Ammo.Value-1

  450. local startpoint=sp.Handle.CFrame*BarrlePos

  451. local mag=(mouse.Hit.p-startpoint).magnitude

  452. local rndm=Vector3.new(math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag),math.random(-(Spread/10)*mag,(Spread/10)*mag))

  453. fire(mouse.Hit.p+rndm)

  454. end

  455. wait(Firerate)

  456. if not automatic then

  457. break

  458. end

  459. end

  460. enabled=true

  461. else

  462. sp.Handle.Trigger:Play()

  463. end

  464. end

  465. function onEquippedLocal(mouse)

  466. if mouse==nil then

  467. print("Mouse not found")

  468. return

  469. end

  470. mouse.Icon=Cursors[1]

  471. mouse.KeyDown:connect(function(key) onKeyDown(key,mouse) end)

  472. mouse.Button1Down:connect(function() onButton1Down(mouse) end)

  473. mouse.Button1Up:connect(function() onButton1Up(mouse) end)

  474. check()

  475. equiped=true

  476. if #Cursors>1 then

  477. while equiped do

  478. t=r.Stepped:wait()

  479. local action=sp.Parent:FindFirstChild("Pose")

  480. if action~=nil then

  481. if sp.Parent.Pose.Value=="Standing" then

  482. Spread=MinSpread

  483. else

  484. Spread=MinSpread+((4/10)*(MaxSpread-MinSpread))

  485. end

  486. else

  487. Spread=MinSpread

  488. end

  489. if t-last<SpreadRate then

  490. Spread=Spread+.1*(MaxSpread-MinSpread)

  491. end

  492. if t-last2<SpreadRate then

  493. Spread=Spread+.1*(MaxSpread-MinSpread)

  494. end

  495. if t-last3<SpreadRate then

  496. Spread=Spread+.1*(MaxSpread-MinSpread)

  497. end

  498. if t-last4<SpreadRate then

  499. Spread=Spread+.1*(MaxSpread-MinSpread)

  500. end

  501. if t-last5<SpreadRate then

  502. Spread=Spread+.1*(MaxSpread-MinSpread)

  503. end

  504. if t-last6<SpreadRate then

  505. Spread=Spread+.1*(MaxSpread-MinSpread)

  506. end

  507. if not reloading then

  508. local percent=(Spread-MinSpread)/(MaxSpread-MinSpread)

  509. for i=0,#Cursors-1 do

  510. if percent>(i/(#Cursors-1))-((1/(#Cursors-1))/2) and percent<(i/(#Cursors-1))+((1/(#Cursors-1))/2) then

  511. mouse.Icon=Cursors[i+1]

  512. end

  513. end

  514. end

  515. wait(Firerate*.9)

  516. end

  517. end

  518. end

  519. function onUnequippedLocal(mouse)

  520. equiped=false

  521. reloading=false

  522. end

  523. sp.Equipped:connect(onEquippedLocal)

  524. sp.Unequipped:connect(onUnequippedLocal)

  525. check()

Gun script for roblox - Pastebin.com (2024)

References

Top Articles
Latest Posts
Article information

Author: Terrell Hackett

Last Updated:

Views: 5868

Rating: 4.1 / 5 (72 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Terrell Hackett

Birthday: 1992-03-17

Address: Suite 453 459 Gibson Squares, East Adriane, AK 71925-5692

Phone: +21811810803470

Job: Chief Representative

Hobby: Board games, Rock climbing, Ghost hunting, Origami, Kabaddi, Mushroom hunting, Gaming

Introduction: My name is Terrell Hackett, I am a gleaming, brainy, courageous, helpful, healthy, cooperative, graceful person who loves writing and wants to share my knowledge and understanding with you.