Advertisement
HowToRoblox

CodesGuiHandler

Dec 18th, 2022
1,516
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.10 KB | None | 0 0
  1. local open = script.Parent:WaitForChild("OpenButtonBase"):WaitForChild("OpenButtonBack"):WaitForChild("OpenButton")
  2. open.Visible = true
  3. local frame = script.Parent:WaitForChild("CodeFrame")
  4. frame.Visible = false
  5. local close = frame:WaitForChild("CloseButtonBack"):WaitForChild("CloseButton")
  6. local succ = frame:WaitForChild("Success")
  7. succ.Text = ""
  8. local redeem = frame:WaitForChild("RedeemFrame"):WaitForChild("RedeemButtonBack"):WaitForChild("RedeemButton")
  9. local enterCode = frame:WaitForChild("CodeBox")
  10.  
  11. local rs = game:GetService("ReplicatedStorage"):WaitForChild("CodesReplicatedStorage")
  12. local re = rs:WaitForChild("RemoteEvent")
  13. local codes = require(rs:WaitForChild("CodesModule"))
  14.  
  15. local fireServerDebounce = false
  16.  
  17. local messageCode = 0
  18.  
  19.  
  20. function success(message)
  21.     succ.Text = message
  22.     succ.TextColor3 = Color3.fromRGB(43, 247, 77)
  23.    
  24.     local generatedCode = math.random(0, 1000000)
  25.     messageCode = generatedCode
  26.    
  27.     task.wait(2)
  28.     if (generatedCode == messageCode) then
  29.         succ.Text = ""
  30.     end
  31. end
  32. function err(message)
  33.     succ.Text = message
  34.     succ.TextColor3 = Color3.fromRGB(235, 43, 43)
  35.    
  36.     local generatedCode = math.random(0, 1000000)
  37.     messageCode = generatedCode
  38.    
  39.     task.wait(2)
  40.     if (generatedCode == messageCode) then
  41.         succ.Text = ""
  42.     end
  43. end
  44.  
  45.  
  46. redeem.MouseButton1Click:Connect(function()
  47.    
  48.     local enteredCode = enterCode.Text
  49.     if string.len(enteredCode) > 0 then
  50.        
  51.         local codeInfo = codes[enteredCode]
  52.        
  53.         if not codeInfo then
  54.             err("Code not found!")
  55.            
  56.         else
  57.             if (codeInfo.expiresAt and os.time() > codeInfo.expiresAt) then
  58.                 err("Code has expired!")
  59.                
  60.             else
  61.                 if (not codeInfo.repeatable and game.Players.LocalPlayer["REDEEMED CODES"]:FindFirstChild(enteredCode)) then
  62.                     err("Already redeemed!")
  63.                    
  64.                 else
  65.                     if not fireServerDebounce then
  66.                         fireServerDebounce = true
  67.                        
  68.                         re:FireServer("REDEEM CODE", enteredCode)
  69.                         task.wait(1)
  70.                        
  71.                         fireServerDebounce = false
  72.                     end
  73.                 end
  74.             end
  75.         end
  76.        
  77.     else
  78.         err("Enter a code!")
  79.     end
  80. end)
  81.  
  82.  
  83. re.OnClientEvent:Connect(function(instruction, value)
  84.     if (instruction == "SUCCESS") then
  85.         success(value)
  86.     elseif (instruction == "ERROR") then
  87.         err(value)
  88.     end
  89. end)
  90.  
  91.  
  92. --Effects (animations + sounds)
  93. local ts = game:GetService("TweenService")
  94.  
  95. local hoverSound = script.Parent:WaitForChild("Sounds"):WaitForChild("Hover")
  96. local clickSound = script.Parent:WaitForChild("Sounds"):WaitForChild("Click")
  97.  
  98. local inQuartTI = TweenInfo.new(0.1, Enum.EasingStyle.Quart, Enum.EasingDirection.In)
  99. local outBounceTI = TweenInfo.new(0.1, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
  100.  
  101.  
  102. function mouseEnter(button, originalPos, offset)
  103.     hoverSound:Play()
  104.     ts:Create(button, inQuartTI, {Position = originalPos + offset, ImageColor3 = Color3.fromRGB(244, 244, 244), BackgroundColor3 = Color3.fromRGB(244, 244, 244)}):Play()
  105. end
  106. function mouseLeave(button, originalPos)
  107.     ts:Create(button, inQuartTI, {Position = originalPos, ImageColor3 = Color3.fromRGB(255, 255, 255), BackgroundColor3 = Color3.fromRGB(255, 255, 255)}):Play()
  108. end
  109. function mouseButton1Down(button, originalPos, offset)
  110.     ts:Create(button, inQuartTI, {Position = originalPos + offset, ImageColor3 = Color3.fromRGB(230, 230, 230), BackgroundColor3 = Color3.fromRGB(230, 230, 230)}):Play()
  111. end
  112. function mouseButton1Up(button, originalPos, offset)
  113.     clickSound:Play()
  114.     ts:Create(button, outBounceTI, {Position = originalPos + offset, ImageColor3 = Color3.fromRGB(244, 244, 244), BackgroundColor3 = Color3.fromRGB(244, 244, 244)}):Play()
  115. end
  116.  
  117.  
  118. local openPos = open.Position
  119. open.MouseEnter:Connect(function()
  120.     mouseEnter(open, openPos, UDim2.new(0, 0, -0.02, 0))
  121. end)
  122. open.MouseLeave:Connect(function()
  123.     mouseLeave(open, openPos)
  124. end)
  125. open.MouseButton1Down:Connect(function()
  126.     mouseButton1Down(open, openPos, UDim2.new(0, 0, 0.03, 0))
  127. end)
  128. open.MouseButton1Up:Connect(function()
  129.     mouseButton1Up(open, openPos, UDim2.new(0, 0, -0.02, 0))
  130. end)
  131.  
  132. local closePos = close.Position
  133. close.MouseEnter:Connect(function()
  134.     mouseEnter(close, closePos, UDim2.new(0.045, 0, -0.044, 0))
  135. end)
  136. close.MouseLeave:Connect(function()
  137.     mouseLeave(close, closePos)
  138. end)
  139. close.MouseButton1Down:Connect(function()
  140.     mouseButton1Down(close, closePos, UDim2.new(-0.072, 0, 0.034, 0))
  141. end)
  142. close.MouseButton1Up:Connect(function()
  143.     mouseButton1Up(close, closePos, UDim2.new(0.045, 0, -0.044, 0))
  144. end)
  145.  
  146. local redeemPos = redeem.Position
  147. redeem.MouseEnter:Connect(function()
  148.     mouseEnter(redeem, redeemPos, UDim2.new(0, 0, 0.06, 0))
  149. end)
  150. redeem.MouseLeave:Connect(function()
  151.     mouseLeave(redeem, redeemPos)
  152. end)
  153. redeem.MouseButton1Down:Connect(function()
  154.     mouseButton1Down(redeem, redeemPos, UDim2.new(0, 0, -0.08, 0))
  155. end)
  156. redeem.MouseButton1Up:Connect(function()
  157.     mouseButton1Up(redeem, redeemPos, UDim2.new(0, 0, 0.06, 0))
  158. end)
  159.  
  160. local enterPos = enterCode.Position
  161. local enterSize = enterCode.Size
  162. local enterH, enterS, enterV = enterCode.BackgroundColor3:ToHSV()
  163. enterCode.MouseEnter:Connect(function()
  164.     hoverSound:Play()
  165.     ts:Create(enterCode, inQuartTI, {Size = UDim2.new(enterSize.X.Scale * 1.02, 0, enterSize.Y.Scale * 1.02, 0), Position = enterPos + UDim2.new(0, 0, -0.01, 0), BackgroundColor3 = Color3.fromHSV(enterH, enterS, enterV + 0.15)}):Play()
  166. end)
  167. enterCode.MouseLeave:Connect(function()
  168.     ts:Create(enterCode, inQuartTI, {Size = enterSize, Position = enterPos, BackgroundColor3 = Color3.fromHSV(enterH, enterS, enterV)}):Play()
  169. end)
  170.  
  171.  
  172. local openFrameTI = TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.Out)
  173. local closeFrameTI = TweenInfo.new(0.4, Enum.EasingStyle.Quart, Enum.EasingDirection.In)
  174. local bounceTI = TweenInfo.new(0.6, Enum.EasingStyle.Bounce, Enum.EasingDirection.Out)
  175.  
  176. local isOpen = false
  177. local openSequenceHappening = false
  178.  
  179. function openFrame()
  180.     frame.Size = UDim2.new(0, 0, 0, 0)
  181.     frame.Position = UDim2.new(0.5, 0, 0.9, 0)
  182.     ts:Create(frame, openFrameTI, {Size = UDim2.new(0.337, 0, 0.492, 0), Position = UDim2.new(0.5, 0, 0.5, 0)}):Play()
  183.     enterCode.Size = UDim2.new(0.46, 0, 0.161, 0)
  184.     ts:Create(enterCode, openFrameTI, {Size = UDim2.new(0.724, 0, 0.161, 0)}):Play()
  185.    
  186.     task.wait(0.1)
  187.     frame.Visible = true
  188.    
  189.     close.Position = closePos + UDim2.new(-0.072, 0, 0.034, 0)
  190.     redeem.Position = redeemPos + UDim2.new(0, 0, -0.08, 0)
  191.     ts:Create(close, bounceTI, {Position = closePos}):Play()
  192.     ts:Create(redeem, bounceTI, {Position = redeemPos}):Play()
  193.    
  194.     task.wait(0.6)
  195. end
  196.  
  197. function closeFrame()
  198.    
  199.     ts:Create(frame, closeFrameTI, {Size = UDim2.new(0, 0, 0, 0), Position = UDim2.new(0.5, 0, 0.9, 0)}):Play()
  200.     ts:Create(enterCode, closeFrameTI, {Size = UDim2.new(0.46, 0, 0.161, 0)}):Play()
  201.    
  202.     task.wait(0.25)
  203.     frame.Visible = false
  204. end
  205.  
  206. open.MouseButton1Click:Connect(function()
  207.    
  208.     if not openSequenceHappening then
  209.         isOpen = not isOpen
  210.         openSequenceHappening = true
  211.        
  212.         if isOpen then
  213.             openFrame()
  214.         else
  215.             closeFrame()
  216.         end
  217.        
  218.         openSequenceHappening = false
  219.     end
  220. end)
  221.  
  222. close.MouseButton1Click:Connect(function()
  223.    
  224.     if not openSequenceHappening then
  225.         isOpen = false
  226.         openSequenceHappening = true
  227.        
  228.         closeFrame()
  229.        
  230.         openSequenceHappening = false
  231.     end
  232. end)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement