Advertisement
Guest User

roblox gun script

a guest
Jul 5th, 2018
6,805
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.18 KB | None | 0 0
  1. You are here: Home / general / How To Make a Gun on Roblox
  2. How To Make a Gun on Roblox
  3. September 16, 2017 By AlvinBLOX
  4.  
  5. How to make a gun on Roblox – Gun Tutorial code download
  6.  
  7.  
  8.  
  9.  
  10. Download the full script for this gun tutorial below.
  11.  
  12. Here’s what your game should look like:
  13.  
  14.  
  15.  
  16.  
  17. --[[ Side Notes:
  18. - Make sure that your game is set up like mine, with all objects added in
  19. - Make sure FilteringEnabled is turned on
  20. - Make sure that you make your own animations and set your animation IDs to your IDs as you cannot currently take
  21. someone else's animations!
  22.  
  23. --Main Script which goes in Server Storage --]]
  24.  
  25.  
  26. local serverStorage = game:GetService("ServerStorage")
  27. local replicatedStorage = game:GetService("ReplicatedStorage")
  28. local KOValue = "Kills"
  29. local WOValue = "Wipeouts"
  30. local damage = 30
  31. replicatedStorage.ShootEvent.OnServerEvent:Connect(function(player,tool,position,part)
  32. if game.Workspace[player.Name].Humanoid.Health <= 0 then
  33. -- The player is dead, do not do anything
  34. else
  35. local distance = (tool.Handle.CFrame.p - position).magnitude
  36. if game.Workspace:FindFirstChild(player.Name.."'s Trajectory") then
  37. game.Workspace:FindFirstChild(player.Name.."'s Trajectory"):Destroy()
  38. end
  39. local trajectory = Instance.new("Part",game.Workspace)
  40. local smoke = serverStorage.SmokeParticle:Clone()
  41. smoke.Parent = tool.Handle
  42. trajectory.BrickColor = BrickColor.new("Institutional white")
  43. trajectory.Material = "SmoothPlastic"
  44. trajectory.Name = player.Name.."'s Trajectory"
  45. trajectory.Transparency = 0.5
  46. trajectory.Anchored = true
  47. trajectory.Locked = true
  48. trajectory.CanCollide = false
  49. trajectory.Size = Vector3.new(0.3,0.3,distance)
  50. for i = 0,distance,6 do
  51. trajectory.CFrame = CFrame.new(tool.Handle.CFrame.p,position) * CFrame.new(0,0,-distance / 2)
  52. wait(0.0001)
  53. end
  54. smoke:Destroy()
  55. if part then
  56. if part.Name == "Head" or part:IsA("Hat") and part.Parent:FindFirstChild("Humanoid").Health > 0 then
  57. -- Boom headshot
  58. replicatedStorage.Headshot:FireClient(player)
  59. damage = 50
  60. end
  61. local humanoid = part.Parent:FindFirstChild("Humanoid")
  62. if not humanoid then
  63. humanoid = part.Parent.Parent:FindFirstChild("Humanoid")
  64. else
  65. humanoid:TakeDamage(damage)
  66. if humanoid.Health <= 0 then
  67. player.leaderstats[KOValue].Value = player.leaderstats[KOValue].Value + 1
  68. game.Players[humanoid.Parent.Name].leaderstats[WOValue].Value = game.Players[humanoid.Parent.Name].leaderstats[WOValue].Value + 1
  69. end
  70. end
  71. wait(0.25)
  72. if trajectory then
  73. trajectory:Destroy()
  74. end
  75. end
  76. end
  77. end)
  78.  
  79. replicatedStorage.EquipAnimation.OnServerEvent:Connect(function(player,animation)
  80. local newAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
  81. newAnim:Play()
  82. replicatedStorage.UnequipAnimation.OnServerEvent:Connect(function(player,animation)
  83. newAnim:Stop()
  84. for i,v in pairs(game.Workspace:GetChildren()) do
  85. if v.Name == player.Name.."'s Trajectory" then
  86. v:Destroy()
  87. end
  88. end
  89. end)
  90. replicatedStorage.Reload.OnServerEvent:Connect(function(player,animation)
  91. newAnim:Stop()
  92. local reloadAnim = game.Workspace[player.Name].Humanoid:LoadAnimation(animation)
  93. reloadAnim:Play()
  94. end)
  95. end)
  96.  
  97. function checkBodyType(player,tool)
  98. if game.Workspace[player.Name]:FindFirstChild("LowerTorso") then -- R15
  99. tool.shoot.AnimationId = "rbxassetid://936531673"
  100. tool.reload.AnimationId = "rbxassetid://937806099"
  101. return "R15"
  102. end
  103. if game.Workspace[player.Name]:FindFirstChild("Torso") then -- R6
  104. tool.shoot.AnimationId = "rbxassetid://1000874313"
  105. tool.reload.AnimationId = "rbxassetid://937933712"
  106. return "R6"
  107. end
  108. end
  109. replicatedStorage.CheckBodyType.OnServerInvoke = checkBodyType
  110.  
  111.  
  112. -- Local Script, which goes inside the tool
  113.  
  114. local tool = script.Parent -- Getting the tool
  115. local player = game:GetService("Players").LocalPlayer -- Getting the player
  116. local mouse = player:GetMouse() -- Getting the mouse
  117. local sound = tool:WaitForChild("Gunfire")
  118. local torso = "" -- Nothing for now.
  119. local reloading = false -- Variable to check if we are currently reloading
  120. local contextActionService = game:GetService("ContextActionService") -- Allow us to cater for Mobile players
  121. local bodytype = nil -- Nil for now but will check whether player is R6 or R15
  122. local difference = 0 -- Difference between position of head and mouse
  123. local replicatedstorage = game:GetService("ReplicatedStorage")
  124. local gungui = tool:WaitForChild("GunGUI")
  125. local bullets = tool:WaitForChild("Bullets")
  126. local reloadtime = 3
  127. -- Remote Events
  128. local equipAnimation = replicatedstorage:WaitForChild("EquipAnimation")
  129. local headshot = replicatedstorage:WaitForChild("Headshot")
  130. local reload2 = replicatedstorage:WaitForChild("Reload")
  131. local shootevent = replicatedstorage:WaitForChild("ShootEvent")
  132. local unequipanimation = replicatedstorage:WaitForChild("UnequipAnimation")
  133. -- Remote Functions
  134. local checkBodyType = replicatedstorage:WaitForChild("CheckBodyType")
  135. local fetchBulletsLeft = replicatedstorage:WaitForChild("FetchBulletsLeft")
  136. -- Find Body Type
  137. function findBodyType() -- Used to determine whether a player is R6 or R15
  138. bodytype = checkBodyType:InvokeServer(tool) -- Invoking the Remotefunction to do a check on the server
  139. print(bodytype)
  140. end
  141. -- Reloading function
  142. function reload()
  143. reloading = true
  144. reload2:FireServer(tool.reload)
  145. mouse.Icon = "http://www.roblox.com/asset?id=936489163"
  146. player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Reloading!"
  147. wait(reloadtime)
  148. bullets.Value = 6
  149. player.PlayerGui:WaitForChild("GunGUI").Bullets.Text = "Bullets: "..bullets.Value
  150. mouse.Icon = "http://www.roblox.com/asset?id=936803874"
  151. equipAnimation:FireServer(tool.shoot)
  152. reloading = false
  153. end
  154. -- When the tool is equipped, the following event will run
  155. tool.Equipped:Connect(function(mouse)
  156. gungui:Clone().Parent = player.PlayerGui -- We are cloning the Gun GUI into the player's PlayerGUI
  157. findBodyType() -- Calling the function above to check the body type.
  158. equipAnimation:FireServer(tool.shoot) -- Calling the equip animation remoteevent so that the server can play the animation
  159. mouse.Icon = "http://www.roblox.com/asset?id=936803874"
  160. mouse.Button1Down:Connect(function()
  161. if bullets.Value <=0 or reloading == true then
  162. -- Don't do anything
  163. else
  164. local head = game.Workspace[player.Name].Head.CFrame.lookVector
  165. local mouse = CFrame.new(game.Workspace[player.Name].Head.Position,mouse.Hit.p).lookVector
  166. difference = (head-mouse)
  167. local ray = Ray.new(tool.Handle.CFrame.p,(player:GetMouse().Hit.p - tool.Handle.CFrame.p).unit*300)
  168. local part,position = game.Workspace:FindPartOnRay(ray,player.Character,false,true)
  169. sound:Play()
  170. if difference.magnitude < 1.33 then
  171. shootevent:FireServer(tool,position,part)
  172. bullets.Value = bullets.Value - 1
  173. end
  174. end
  175. end)
  176. local reloadMobileButton = contextActionService:BindAction("ReloadBtn",reload,true,"r")
  177. contextActionService:SetPosition("ReloadBtn",UDim2.new(0.72,-25,0.20,-25))
  178. contextActionService:SetImage("ReloadBtn","http://www.roblox.com/asset/?id=10952419")
  179. end)
  180. tool.Unequipped:Connect(function()
  181. mouse.Icon = ""
  182. unequipanimation:FireServer(tool.shoot)
  183. player.PlayerGui.GunGUI:Destroy()
  184. contextActionService:UnbindAction("ReloadBtn")
  185. end)
  186. headshot.OnClientEvent:Connect(function()
  187. player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(0.5,-100,0.5,-25), "Out","Quint",0.3)
  188. wait(1.5)
  189. player.PlayerGui.GunGUI.Headshot:TweenPosition(UDim2.new(-1,0,0.5,-25), "In","Quint",0.4)
  190. wait(0.5)
  191. player.PlayerGui.GunGUI.Headshot.Position = UDim2.new(1.5,0,0.5,-25)
  192. end)
  193. — Leaderboard code which goes inside ServerScriptService, optional to include, you can add your own if you want but bear in mind the gun won’t work unless you have some sort of leaderboard with a Kills and Wipeouts stat
  194.  
  195. game.Players.PlayerAdded:Connect(function(player)
  196. local leaderstats = Instance.new("IntValue",player)
  197. leaderstats.Name = "leaderstats"
  198.  
  199. local kills = Instance.new("IntValue",leaderstats)
  200. kills.Name = "Kills"
  201. kills.Value = 0
  202.  
  203. local wipeouts = Instance.new("IntValue",leaderstats)
  204. wipeouts.Name = "Wipeouts"
  205. wipeouts.Value = 0
  206.  
  207. -- Data saving code goes here if you need it
  208. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement