Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.08 KB | None | 0 0
  1. -- @ bakers
  2.  
  3. local ReplicatedStorage = game:GetService("ReplicatedStorage")
  4. local ServerStorage = game:GetService("ServerStorage")
  5.  
  6. local Players = game:GetService("Players")
  7.  
  8. local Network = ReplicatedStorage.Network
  9. local Assets = ReplicatedStorage.Assets
  10.  
  11. local Module, Modules = {}, {}
  12.  
  13. --
  14.  
  15. local Player = Players.LocalPlayer
  16.  
  17. local Connections = {}
  18.  
  19. -- all interactable objects
  20. -- used for raycast whitelist
  21. local Interactables = workspace.Interactables:GetDescendants()
  22.  
  23. -- distance from interactable item
  24. local Distance = 3
  25.  
  26. local UI
  27.  
  28. local Playing, currentInteraction
  29.  
  30. local function CreateRegion3From(Position, Size)
  31.     local SizeOffset = Size/2
  32.     local Point1 = Position - SizeOffset
  33.     local Point2 = Position + SizeOffset
  34.     return Region3.new(Point1, Point2)
  35. end
  36.  
  37. local function cancelCurrentInteraction()
  38.     UI:Destroy()
  39.     currentInteraction = nil
  40.     Playing = false
  41. end
  42.  
  43. local function cancelConnections()
  44.     for _,Connection in pairs(Connections) do
  45.         Connection:Disconnect()
  46.     end
  47. end
  48.  
  49. local function Keyboard(Input, Down, Target)
  50.  
  51.     if (not Target) then return end
  52.    
  53.     if (Input == Enum.KeyCode.E) then
  54.         if (Down) then
  55.            
  56.             if (Target.InUse.Value) then
  57.                 Modules.GFX:Glow(UI.Body, {
  58.                     Size = UDim2.new(0, UI.AbsoluteSize.X, 0, UI.AbsoluteSize.Y);
  59.                     Position = UDim2.new(0, UI.AbsolutePosition.X, 0, UI.AbsolutePosition.Y);
  60.                     BackgroundColor3 = Color3.fromRGB(195, 0, 0)
  61.                 })
  62.                 return
  63.             end
  64.            
  65.             Modules[Target.Name]:Use(Target)
  66.            
  67.             -- fill bar
  68.             UI.Body.Fill:TweenSize(UDim2.new(1, 0, 1, 0), "Out", "Linear", Target.Interactable.Value, true, function(Status)
  69.                 if (Status == Enum.TweenStatus.Completed) then
  70.                     Modules[Target.Name]:Used(Target, true)
  71.                     cancelCurrentInteraction()
  72.                 end
  73.             end)
  74.         else
  75.             -- empty bar
  76.             UI.Body.Fill:TweenSize(UDim2.new(0, 0, 1, 0), "Out", "Linear", .5, true, function(Status)
  77.                 if (Status == Enum.TweenStatus.Completed) then
  78.                     Modules[Target.Name]:Used(Target, false)
  79.                     cancelCurrentInteraction()
  80.                 end
  81.             end)
  82.         end
  83.     end
  84. end
  85.  
  86. function Module:Run()
  87.    
  88.     local Character = Player.Character
  89.    
  90.     local Region = CreateRegion3From(Character.HumanoidRootPart.Position, Vector3.new(Distance, Distance, Distance))
  91.    
  92.     local Parts = workspace:FindPartsInRegion3WithWhiteList(Region, Interactables)
  93.     local Part = Parts[1]
  94.    
  95.     if (Part) then
  96.         if (not (Part:FindFirstChild("Interactable"))) then return end
  97.         if (Playing) then return end
  98.         if (Part == currentInteraction) then
  99.             return
  100.         else   
  101.             cancelConnections()
  102.             currentInteraction = Part
  103.             Playing = true
  104.         end
  105.  
  106.         if (UI) then
  107.             UI:Destroy()
  108.         end
  109.  
  110.         UI = Assets.Interface.Billboard.Interact:Clone()
  111.         UI.Parent = currentInteraction
  112.        
  113.         -- detect whether the user is holding 'e'
  114.         Connections.Input = Network.Bindables.Events.Player.Keyboard.Event:connect(function(Input, Down)
  115.             Keyboard(Input, Down, currentInteraction)
  116.         end)
  117.     else
  118.         cancelConnections()
  119.         if (currentInteraction) then
  120.             cancelCurrentInteraction()
  121.         end
  122.     end
  123. end
  124.  
  125. function Module.Initialise(...)
  126.     Modules = ...
  127. end
  128.  
  129. return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement