Advertisement
LawMixer

newDoorHandler(functions is where I register the functions)

May 22nd, 2021
1,030
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.71 KB | None | 0 0
  1. -- services
  2. local players = game:GetService('Players')
  3. local tweenService = game:GetService('TweenService')
  4. local replicatedStorage = game:GetService('ReplicatedStorage')
  5. local inputService = game:GetService('UserInputService')
  6. local runService = game:GetService('RunService')
  7.  
  8. -- modules
  9. local functions = require(script:WaitForChild('Functions'))
  10.  
  11. -- player
  12. local player = players.LocalPlayer
  13.  
  14. -- remotes
  15. local doorEvent = replicatedStorage:WaitForChild('DoorEvent')
  16.  
  17. -- objects
  18. local doors = workspace:WaitForChild('Doors')
  19. local doorModules = replicatedStorage:WaitForChild('DoorModules')
  20.  
  21. -- variables
  22. local ACTIVATION_KEY = Enum.KeyCode.G
  23. local IMAGE_CLEARANCE = {
  24.     [0] = nil,
  25.     [1] = 4813020837,
  26.     [2] = 4813021130,
  27.     [3] = 4813021352,
  28.     [4] = 4813021933,
  29.     [5] = 4813022124,
  30.     [6] = 4813022331,
  31.     [7] = 4813023753,
  32. }
  33. local lastDoor = nil
  34. local doorData = {}
  35.  
  36. -- events
  37. inputService.InputBegan:connect(function(input, processed)
  38.     --if not processed then
  39.         if (input.KeyCode == ACTIVATION_KEY) then
  40.         local closest = functions.GetClosestDoor(player, doors)
  41.         print(functions:GetClosestDoor(player, doors))
  42.         local model = closest.doors
  43.         local model2 = workspace.Doors.breach_door.Door1.Main.Animator.Tweens
  44.         local opened = model2:FindFirstChild('DoorOpen')
  45.         local module = require(doorModules[model.Name])
  46.  
  47.         if (closest.distance <= module.MAX_DISTANCE) and (tick() - doorData[model].lastActivation) >= module.DOOR_SPEED then
  48.                 doorData[model].opened = (not opened.Value)
  49.  
  50.                 doorEvent:FireServer('ToggleDoor', model, doorData[model].opened)
  51.                
  52.                 doorData[model].lastActivation = tick()
  53.             end
  54.         end
  55.     --end
  56. end)
  57.  
  58. runService.Heartbeat:connect(function()
  59.     local closest = functions.GetClosestDoor(player, doors)
  60.     print(closest)
  61.     local model = closest.door
  62.     local module = require(doorModules[model.Name])
  63.    
  64.     if closest then
  65.         local doorClearance = (model:FindFirstChild('clr', true) and model:FindFirstChild('clr', true).Value) or -1
  66.         local mainPart = model:FindFirstChild('MainPart', true) or closest.trigger
  67.         local oldUI = (lastDoor and lastDoor:FindFirstChild('DoorUI', true)) or closest.trigger:FindFirstChild('DoorUI')
  68.        
  69.         doorClearance = math.min(doorClearance, #IMAGE_CLEARANCE)
  70.  
  71.         if (model ~= lastDoor) and oldUI then
  72.             oldUI:Destroy()
  73.         end
  74.  
  75.         if (closest.distance < module.MAX_DISTANCE) then
  76.             if not oldUI then
  77.                 local newUI = script.DoorUI:Clone()
  78.                 local frame = newUI.Frame
  79.                 local uiScale = frame.UIScale
  80.                 local header = frame.Header
  81.                 local clearance = frame.Clearance
  82.                
  83.                 tweenService:Create(uiScale, TweenInfo.new(.15), {Scale = 1}):Play()
  84.                
  85.                 if (doorClearance > 0) then
  86.                     header.Size = UDim2.new(.7, 0, .435, 0)
  87.                     clearance.Visible = true
  88.                     clearance.Image = ('rbxassetid://' .. IMAGE_CLEARANCE[doorClearance])
  89.                 else
  90.                     clearance.Visible = false
  91.                     header.Size = UDim2.new(.95, 0, .435, 0)
  92.                 end
  93.                
  94.                 newUI.Parent = closest.trigger
  95.             end
  96.         elseif oldUI then
  97.             oldUI:Destroy()
  98.         end
  99.     end
  100.    
  101.     lastDoor = model or nil
  102. end)
  103.  
  104. doorEvent.OnClientEvent:connect(function(arg, ...)
  105.     local args = {...}
  106.    
  107.     if (arg == 'Tween') then
  108.         local model = args[1]
  109.         local part = args[2]
  110.         local cframe = args[3]
  111.         local bool = args[4]
  112.         local module = require(doorModules:FindFirstChild(model.Name))
  113.         local info = module.TWEEN_INFO
  114.  
  115.         if (model:IsDescendantOf(doors)) then
  116.             tweenService:Create(part, TweenInfo.new(module.DOOR_SPEED, info.EasingStyle, info.EasingDirection), {
  117.                 CFrame = cframe
  118.             }):Play()
  119.         end
  120.     end
  121. end)
  122.  
  123. --for _, door in next, doors:GetChildren() do
  124. --  local value = door:WaitForChild('DoorOpened').Value
  125.    
  126. --  doorData[door] = {
  127. --      opened = value,
  128. --      lastActivation = tick()
  129. --  }
  130. --end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement