Advertisement
ERROR_CODE

Flashlight

Aug 2nd, 2023
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.87 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. Part1 = Instance.new("Part")
  19. Sound2 = Instance.new("Sound")
  20. SpecialMesh3 = Instance.new("SpecialMesh")
  21. SpotLight4 = Instance.new("SpotLight")
  22. LocalScript5 = Instance.new("LocalScript")
  23. LocalScript6 = Instance.new("LocalScript")
  24. ModuleScript7 = Instance.new("ModuleScript")
  25. Tool0.Name = "Flashlight"
  26. Tool0.Parent = mas
  27. Tool0.TextureId = "http://www.roblox.com/asset/?id=115955232"
  28. Tool0.Grip = CFrame.new(0.100000001, -0.400000006, 0, 1, 0, 0, 0, 1, 0, 0, 0, 1)
  29. Tool0.GripPos = Vector3.new(0.10000000149011612, -0.4000000059604645, 0)
  30. Tool0.ToolTip = "Flashlight"
  31. Part1.Name = "Handle"
  32. Part1.Parent = Tool0
  33. Part1.CFrame = CFrame.new(10.9154053, 0.25, -18.9892578, 0, 0, 1, 0, 1, 0, -1, 0, 0)
  34. Part1.Orientation = Vector3.new(0, 90, 0)
  35. Part1.Position = Vector3.new(10.9154052734375, 0.25, -18.9892578125)
  36. Part1.Rotation = Vector3.new(0, 90, 0)
  37. Part1.Color = Color3.new(0.960784, 0.803922, 0.188235)
  38. Part1.Size = Vector3.new(0.5, 0.5, 2)
  39. Part1.BottomSurface = Enum.SurfaceType.Smooth
  40. Part1.BrickColor = BrickColor.new("Bright yellow")
  41. Part1.TopSurface = Enum.SurfaceType.Smooth
  42. Part1.brickColor = BrickColor.new("Bright yellow")
  43. Part1.FormFactor = Enum.FormFactor.Custom
  44. Part1.formFactor = Enum.FormFactor.Custom
  45. Sound2.Parent = Part1
  46. Sound2.SoundId = "http://www.roblox.com/asset/?id=115959318"
  47. Sound2.Volume = 1
  48. SpecialMesh3.Parent = Part1
  49. SpecialMesh3.MeshId = "http://www.roblox.com/asset/?id=115955313"
  50. SpecialMesh3.Scale = Vector3.new(0.699999988079071, 0.699999988079071, 0.699999988079071)
  51. SpecialMesh3.TextureId = "http://www.roblox.com/asset?id=115955343"
  52. SpecialMesh3.MeshType = Enum.MeshType.FileMesh
  53. SpotLight4.Parent = Part1
  54. SpotLight4.Enabled = false
  55. SpotLight4.Range = 40
  56. SpotLight4.Brightness = 10
  57. SpotLight4.Shadows = true
  58. LocalScript5.Parent = Tool0
  59. table.insert(cors,sandbox(LocalScript5,function()
  60. player = game.Players.LocalPlayer
  61. tool = script.Parent
  62.  
  63. repeat wait() until player
  64.  
  65. tool.Equipped:connect(function(mouse)
  66.     mouse.Button1Down:connect(function()
  67.         tool.Handle.SpotLight.Enabled = not tool.Handle.SpotLight.Enabled
  68.         tool.Handle.Sound:Play()
  69.     end)
  70. end)
  71. end))
  72. LocalScript6.Parent = Tool0
  73. table.insert(cors,sandbox(LocalScript6,function()
  74. -- Variables for services
  75. local render = game:GetService("RunService").RenderStepped
  76. local contextActionService = game:GetService("ContextActionService")
  77. local userInputService = game:GetService("UserInputService")
  78.  
  79. local player = game.Players.LocalPlayer
  80. local mouse = player:GetMouse()
  81. local Tool = script.Parent
  82.  
  83. -- Variables for Module Scripts
  84. local screenSpace = require(Tool:WaitForChild("ScreenSpace"))
  85.  
  86. local connection
  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.         end
  161.     end
  162. end
  163.  
  164. -- Function to bind to render stepped if player is on PC
  165. local function pcFrame()
  166.     frame(mouse.Hit.p)
  167. end
  168.  
  169. -- Function to bind to touch moved if player is on mobile
  170. local function mobileFrame(touch, processed)
  171.     -- Check to see if the touch was on a UI element. If so, we don't want to update anything
  172.     if not processed then
  173.         -- Calculate touch position in world space. Uses Stravant's ScreenSpace Module script
  174.         -- to create a ray from the camera.
  175.         local test = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1)
  176.         local nearPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y, 1))
  177.         nearPos = game.Workspace.CurrentCamera.CoordinateFrame.p - nearPos
  178.         local farPos = screenSpace.ScreenToWorld(touch.Position.X, touch.Position.Y,50)
  179.         farPos = game.Workspace.CurrentCamera.CoordinateFrame:vectorToWorldSpace(farPos) * -1
  180.         if farPos.magnitude > 900 then
  181.             farPos = farPos.unit * 900
  182.         end
  183.         local ray = Ray.new(nearPos, farPos)
  184.         local part, pos = game.Workspace:FindPartOnRay(ray, player.Character)
  185.        
  186.         -- if a position was found on the ray then update the character's rotation
  187.         if pos then
  188.             frame(pos)
  189.         end
  190.     end
  191. end
  192.  
  193. local function OnActivated()
  194.     local myModel = player.Character
  195.     if Tool.Enabled and myModel and myModel:FindFirstChild('Humanoid') and myModel.Humanoid.Health > 0 then
  196.         Tool.Enabled = false
  197.         game.ReplicatedStorage.ROBLOX_RocketFireEvent:FireServer(mouse.Hit.p)
  198.         wait(2)
  199.  
  200.         Tool.Enabled = true
  201.     end
  202. end
  203.  
  204. local oldIcon = nil
  205. -- Function to bind to equip event
  206. local function equip()
  207.     local torso = player.Character.Torso
  208.    
  209.     -- Setup joint variables
  210.     neck = torso.Neck  
  211.     oldNeckC0 = neck.C0
  212.     shoulder = torso:FindFirstChild("Right Shoulder")
  213.     oldShoulderC0 = shoulder.C0
  214.    
  215.     -- Remember old mouse icon and update current
  216.     oldIcon = mouse.Icon
  217.     mouse.Icon = "rbxasset://textures\\GunCursor.png"
  218.    
  219.     -- Bind TouchMoved event if on mobile. Otherwise connect to renderstepped
  220.     if userInputService.TouchEnabled then
  221.         connection = userInputService.TouchMoved:connect(mobileFrame)
  222.     else
  223.         connection = render:connect(pcFrame)
  224.     end
  225.    
  226.     -- Bind TouchStarted and TouchEnded. Used to determine if character should rotate
  227.     -- during touch input
  228.     userInputService.TouchStarted:connect(function(touch, processed)
  229.         mobileShouldTrack = not processed
  230.     end)   
  231.     userInputService.TouchEnded:connect(function(touch, processed)
  232.         mobileShouldTrack = false
  233.     end)
  234.  
  235.     -- If game uses filtering enabled then need to update server while tool is
  236.     -- held by character.
  237.     if workspace.FilteringEnabled then
  238.         while connection do
  239.             wait()
  240.             game.ReplicatedStorage.ROBLOX_RocketUpdateEvent:FireServer(neck.C0, shoulder.C0)
  241.         end
  242.     end
  243. end
  244.  
  245. -- Function to bind to Unequip event
  246. local function unequip()
  247.     if connection then connection:disconnect() end
  248.    
  249.     mouse.Icon = oldIcon
  250.    
  251.     neck.C0 = oldNeckC0
  252.     shoulder.C0 = oldShoulderC0
  253. end
  254.  
  255. -- Bind tool events
  256. Tool.Equipped:connect(equip)
  257. Tool.Unequipped:connect(unequip)
  258. Tool.Activated:connect(OnActivated)
  259. end))
  260. ModuleScript7.Name = "ScreenSpace"
  261. ModuleScript7.Parent = Tool0
  262. table.insert(cors,sandbox(ModuleScript7,function()
  263. local PlayerMouse = Game:GetService('Players').LocalPlayer:GetMouse()
  264.  
  265. local ScreenSpace = {}
  266.  
  267. -- Getter functions, with a couple of hacks for Ipad pre-focus.
  268. function ScreenSpace.ViewSizeX()
  269.     local x = PlayerMouse.ViewSizeX
  270.     local y = PlayerMouse.ViewSizeY
  271.     if x == 0 then
  272.         return 1024
  273.     else
  274.         if x > y then
  275.             return x
  276.         else
  277.             return y
  278.         end
  279.     end
  280. end
  281.  
  282. function ScreenSpace.ViewSizeY()
  283.     local x = PlayerMouse.ViewSizeX
  284.     local y = PlayerMouse.ViewSizeY
  285.     if y == 0 then
  286.         return 768
  287.     else
  288.         if x > y then
  289.             return y
  290.         else
  291.             return x
  292.         end
  293.     end
  294. end
  295.  
  296. -- Nice getter for aspect ratio. Due to the checks in the ViewSize functions this
  297. -- will never fail with a divide by zero error.
  298. function ScreenSpace.AspectRatio()
  299.     return ScreenSpace.ViewSizeX() / ScreenSpace.ViewSizeY()
  300. end
  301.  
  302. -- WorldSpace -> ScreenSpace. Raw function taking a world position and giving you the
  303. -- screen position.
  304. function ScreenSpace.WorldToScreen(at)
  305.     local point = Workspace.CurrentCamera.CoordinateFrame:pointToObjectSpace(at)
  306.     local aspectRatio = ScreenSpace.AspectRatio()
  307.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  308.     local wfactor = aspectRatio*hfactor
  309.     --
  310.     local x = (point.x/point.z) / -wfactor
  311.     local y = (point.y/point.z) /  hfactor
  312.     --
  313.     return Vector2.new(ScreenSpace.ViewSizeX()*(0.5 + 0.5*x), ScreenSpace.ViewSizeY()*(0.5 + 0.5*y))
  314. end
  315.  
  316. -- ScreenSpace -> WorldSpace. Raw function taking a screen position and a depth and
  317. -- converting it into a world position.
  318. function ScreenSpace.ScreenToWorld(x, y, depth)
  319.     local aspectRatio = ScreenSpace.AspectRatio()
  320.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  321.     local wfactor = aspectRatio*hfactor
  322.     --
  323.     local xf, yf = x/ScreenSpace.ViewSizeX()*2 - 1, y/ScreenSpace.ViewSizeY()*2 - 1
  324.     local xpos = xf * -wfactor * depth
  325.     local ypos = yf *  hfactor * depth
  326.     --
  327.     return Vector3.new(xpos, ypos, depth)
  328. end
  329.  
  330. -- ScreenSize -> WorldSize
  331. function ScreenSpace.ScreenWidthToWorldWidth(screenWidth, depth)   
  332.     local aspectRatio = ScreenSpace.AspectRatio()
  333.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  334.     local wfactor = aspectRatio*hfactor
  335.     local sx = ScreenSpace.ViewSizeX()
  336.     --
  337.     return -(screenWidth / sx) * 2 * wfactor * depth
  338. end
  339. function ScreenSpace.ScreenHeightToWorldHeight(screenHeight, depth)
  340.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  341.     local sy = ScreenSpace.ViewSizeY()
  342.     --
  343.     return -(screenHeight / sy) * 2 * hfactor * depth
  344. end
  345.  
  346. -- WorldSize -> ScreenSize
  347. function ScreenSpace.WorldWidthToScreenWidth(worldWidth, depth)
  348.     local aspectRatio = ScreenSpace.AspectRatio()
  349.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  350.     local wfactor = aspectRatio*hfactor
  351.     local sx = ScreenSpace.ViewSizeX()
  352.     --
  353.     return -(worldWidth * sx) / (2 * wfactor * depth)
  354. end
  355. function ScreenSpace.WorldHeightToScreenHeight(worldHeight, depth)
  356.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  357.     local sy = ScreenSpace.ViewSizeY()
  358.     --
  359.     return -(worldHeight * sy) / (2 * hfactor * depth)
  360. end
  361.  
  362. -- WorldSize + ScreenSize -> Depth needed
  363. function ScreenSpace.GetDepthForWidth(screenWidth, worldWidth)
  364.     local aspectRatio = ScreenSpace.AspectRatio()
  365.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  366.     local wfactor = aspectRatio*hfactor
  367.     local sx, sy = ScreenSpace.ViewSizeX(), ScreenSpace.ViewSizeY()
  368.     --
  369.     return -(sx * worldWidth) / (screenWidth * 2 * wfactor)
  370. end
  371. function ScreenSpace.GetDepthForHeight(screenHeight, worldHeight)
  372.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  373.     local sy = ScreenSpace.ViewSizeY()
  374.     --
  375.     return -(sy * worldHeight) / (screenHeight * 2 * hfactor)  
  376. end
  377.  
  378. -- ScreenSpace -> WorldSpace. Taking a screen height, and a depth to put an object
  379. -- at, and returning a size of how big that object has to be to appear that size
  380. -- at that depth.
  381. function ScreenSpace.ScreenToWorldByHeightDepth(x, y, screenHeight, depth)
  382.     local aspectRatio = ScreenSpace.AspectRatio()
  383.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  384.     local wfactor = aspectRatio*hfactor
  385.     local sx, sy = ScreenSpace.ViewSizeX(), ScreenSpace.ViewSizeY()
  386.     --
  387.     local worldHeight = -(screenHeight/sy) * 2 * hfactor * depth
  388.     --
  389.     local xf, yf = x/sx*2 - 1, y/sy*2 - 1
  390.     local xpos = xf * -wfactor * depth
  391.     local ypos = yf *  hfactor * depth
  392.     --
  393.     return Vector3.new(xpos, ypos, depth), worldHeight
  394. end
  395.  
  396. -- ScreenSpace -> WorldSpace. Taking a screen width, and a depth to put an object
  397. -- at, and returning a size of how big that object has to be to appear that size
  398. -- at that depth.
  399. function ScreenSpace.ScreenToWorldByWidthDepth(x, y, screenWidth, depth)
  400.     local aspectRatio = ScreenSpace.AspectRatio()
  401.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  402.     local wfactor = aspectRatio*hfactor
  403.     local sx, sy = ScreenSpace.ViewSizeX(), ScreenSpace.ViewSizeY()
  404.     --
  405.     local worldWidth = (screenWidth/sx) * 2 * -wfactor * depth
  406.     --
  407.     local xf, yf = x/sx*2 - 1, y/sy*2 - 1
  408.     local xpos = xf * -wfactor * depth
  409.     local ypos = yf *  hfactor * depth
  410.     --
  411.     return Vector3.new(xpos, ypos, depth), worldWidth
  412. end
  413.  
  414. -- ScreenSpace -> WorldSpace. Taking a screen height that you want that object to be
  415. -- and a world height that is the size of that object, and returning the position to
  416. -- put that object at to satisfy those.
  417. function ScreenSpace.ScreenToWorldByHeight(x, y, screenHeight, worldHeight)
  418.     local aspectRatio = ScreenSpace.AspectRatio()
  419.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  420.     local wfactor = aspectRatio*hfactor
  421.     local sx, sy = ScreenSpace.ViewSizeX(), ScreenSpace.ViewSizeY()
  422.     --
  423.     local depth = - (sy * worldHeight) / (screenHeight * 2 * hfactor)
  424.     --
  425.     local xf, yf = x/sx*2 - 1, y/sy*2 - 1
  426.     local xpos = xf * -wfactor * depth
  427.     local ypos = yf *  hfactor * depth
  428.     --
  429.     return Vector3.new(xpos, ypos, depth)
  430. end
  431.  
  432. -- ScreenSpace -> WorldSpace. Taking a screen width that you want that object to be
  433. -- and a world width that is the size of that object, and returning the position to
  434. -- put that object at to satisfy those.
  435. function ScreenSpace.ScreenToWorldByWidth(x, y, screenWidth, worldWidth)
  436.     local aspectRatio = ScreenSpace.AspectRatio()
  437.     local hfactor = math.tan(math.rad(Workspace.CurrentCamera.FieldOfView)/2)
  438.     local wfactor = aspectRatio*hfactor
  439.     local sx, sy = ScreenSpace.ViewSizeX(), ScreenSpace.ViewSizeY()
  440.     --
  441.     local depth = - (sx * worldWidth) / (screenWidth * 2 * wfactor)
  442.     --
  443.     local xf, yf = x/sx*2 - 1, y/sy*2 - 1
  444.     local xpos = xf * -wfactor * depth
  445.     local ypos = yf *  hfactor * depth
  446.     --
  447.     return Vector3.new(xpos, ypos, depth)
  448. end
  449.  
  450. return ScreenSpace
  451.  
  452.  
  453.  
  454.  
  455. end))
  456. for i,v in pairs(mas:GetChildren()) do
  457.     v.Parent = game:GetService("Players").LocalPlayer.Backpack
  458.     pcall(function() v:MakeJoints() end)
  459. end
  460. mas:Destroy()
  461. for i,v in pairs(cors) do
  462.     spawn(function()
  463.         pcall(v)
  464.     end)
  465. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement