Advertisement
Herobrinekid2

Gui controller/module(Client)

Jan 4th, 2025 (edited)
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.19 KB | Source Code | 0 0
  1. local Module = {IsActive = false}
  2. local PlayerGui = game.Players.LocalPlayer.PlayerGui
  3. local DebrisClass = require(game.ReplicatedStorage.Debris)
  4. local Config = require(game.ReplicatedStorage["Code door config"])
  5. local SFX = game.SoundService.SFX
  6. local Remote = game.ReplicatedStorage["Validate code"]
  7.  
  8. local function Tween(Item: Instance, Info: TweenInfo, PropertyTable: {[string]: any}): Tween
  9.     local T = game.TweenService:Create(Item,Info,PropertyTable)
  10.    
  11.     T:Play()
  12.     return T
  13. end
  14.  
  15. function Module.Enable()
  16.     Module.IsActive = true
  17.     local Gui = PlayerGui["Code gui"]
  18.     local Frame = Gui.Frame
  19.     local Buttons = Frame.Buttons
  20.     local Numpad = Buttons.Numpad
  21.     local Display = Frame.Display
  22.     local Invalid = Frame.Invalid
  23.    
  24.     local Debris = DebrisClass.new()
  25.     local Code = table.create(Config.NumDigits,"*")
  26.     local At = 0
  27.     local ButtonTweenInfo = TweenInfo.new(.1,Enum.EasingStyle.Linear,Enum.EasingDirection.Out)
  28.     local WaitingThreads = {} -- Only really stores the thread for "SignalInvalidCode" xD
  29.    
  30.     local function SignalInvalidCode()
  31.         if Invalid.Visible then return end
  32.         local Thread
  33.        
  34.         SFX.Error:Play()
  35.         Invalid.Visible = true
  36.         Thread = task.delay(Config.InvalidCodeVisibleDuration,function()
  37.             Invalid.Visible = false
  38.             table.remove(WaitingThreads,table.find(WaitingThreads,Thread))
  39.         end)
  40.         table.insert(WaitingThreads,Thread)
  41.     end
  42.    
  43.     local function Close()
  44.         Module.IsActive = false
  45.         Frame.Visible = false
  46.         Invalid.Visible = false -- In case its visible.
  47.         Debris.Clean()
  48.         for i = #WaitingThreads,1,-1 do local Thread = WaitingThreads[i] table.remove(WaitingThreads,i) task.cancel(Thread)  end
  49.     end
  50.    
  51.     local function UpdateDisplay(Method: string, Digit: string)
  52.         if Method == "Add" then
  53.             if At + 1 > Config.NumDigits then return end
  54.             At += 1
  55.         end
  56.         Code[At] = Digit
  57.         Display.Text = table.concat(Code,"")
  58.         if Method == "Back" then -- We got this in its own if statement cuz having it togheter with the first one causes it to not update the display properly.
  59.             if At - 1 < 0 then return end
  60.             At -= 1
  61.         end
  62.     end
  63.    
  64.     local function TweenButton(Btn: TextButton)
  65.         if Btn:HasTag("Tweening") then return end
  66.         local OldSize = Btn.Size
  67.         Btn:AddTag("Tweening")
  68.        
  69.         Tween(Btn,ButtonTweenInfo,{Size = UDim2.fromScale(OldSize.X.Scale-0.08,OldSize.Y.Scale-0.08)}).Completed:Once(function()
  70.             Tween(Btn,ButtonTweenInfo,{Size = OldSize}).Completed:Wait()
  71.             Btn:RemoveTag("Tweening")
  72.         end)
  73.     end
  74.    
  75.     Frame.Visible = true
  76.     Display.Text = table.concat(Code,"")
  77.     for _, v in Numpad:GetChildren() do
  78.         if not tonumber(v.Name) then continue end
  79.         local Btn = v.Button
  80.        
  81.         Debris.Add(Btn.MouseButton1Click:Connect(function()
  82.             SFX.Click:Play()
  83.             UpdateDisplay("Add",Btn.Text)
  84.             TweenButton(Btn)
  85.         end))
  86.     end
  87.     Debris.Add({
  88.         Numpad["Go back"].Button.MouseButton1Click:Connect(function()
  89.             SFX.Submit:Play()
  90.             UpdateDisplay("Back","*")
  91.         end),
  92.         Numpad.Submit.Button.MouseButton1Click:Connect(function()
  93.             SFX.Submit:Play()
  94.             if Remote:InvokeServer(tonumber(table.concat(Code,""))) then
  95.                 Close()
  96.             else
  97.                 SignalInvalidCode()
  98.             end
  99.         end),
  100.         Buttons.Close.MouseButton1Click:Connect(function() SFX.Submit:Play() Close() end)
  101.     })
  102. end
  103.  
  104.  
  105. return Module
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement