Advertisement
PlayStationUser99

JToH Remastered Part 4.5: MainScript

May 31st, 2025
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.08 KB | None | 0 0
  1. local players = game:GetService("Players")
  2. local runService = game:GetService("RunService")
  3. local replicatedStorage = game:GetService("ReplicatedStorage")
  4. local userInputService = game:GetService("UserInputService")
  5.  
  6. local remoteEvents = replicatedStorage:WaitForChild("RemoteEvents")
  7. local winpadTouchedEvent = remoteEvents:WaitForChild("WinpadTouched")
  8.  
  9. local remoteFunctions = replicatedStorage:WaitForChild("RemoteFunctions")
  10. local getTimerFunction = remoteFunctions:WaitForChild("GetTimer")
  11.  
  12. local player = players.LocalPlayer
  13.  
  14. local playerGui = player:WaitForChild("PlayerGui")
  15. local localGui = playerGui:WaitForChild("LocalGUI")
  16. local timerFrame = localGui:WaitForChild("Timer")
  17. local timerMain = timerFrame:WaitForChild("TimerMain")
  18. local currentTowerLabel = timerFrame:WaitForChild("CurrentTower")
  19. local restartLabel = localGui:WaitForChild("RestartLabel")
  20.  
  21. local portals = workspace:WaitForChild("Portals")
  22. local towers = workspace:WaitForChild("Towers")
  23.  
  24. local timerRunning = false
  25. local currentTower = ""
  26.  
  27. local restartKey = Enum.KeyCode.R
  28. local restartKeyHolding = false
  29. local restartTime = 1
  30. local restartHold = 0
  31.  
  32. local timer = 0
  33.  
  34. function SetupPortals()
  35.     for i, portal in pairs(portals:GetChildren()) do
  36.         if portal:FindFirstChild("Tower") then
  37.             portal.Touched:Connect(function(hit)
  38.  
  39.                 if currentTower ~= "" then return end
  40.  
  41.                 if hit and hit.Parent:FindFirstChild("Humanoid") then
  42.                     if hit.Parent == player.Character then
  43.                         currentTower = portal.Tower.Value
  44.  
  45.                         hit.Parent.HumanoidRootPart.CFrame = towers:FindFirstChild(portal.Tower.Value).SpawnLocation.CFrame + Vector3.new(0, 1, 0)
  46.  
  47.                         timerRunning = true
  48.  
  49.                         currentTowerLabel.TextColor3 = portal.Color
  50.                     end
  51.                 end
  52.             end)
  53.         end
  54.     end
  55.    
  56.     portals.ChildAdded:Connect(function(portal)
  57.         if portal:FindFirstChild("Tower") then
  58.             portal.Touched:Connect(function(hit)
  59.  
  60.                 if currentTower ~= "" then return end
  61.  
  62.                 if hit and hit.Parent:FindFirstChild("Humanoid") then
  63.                     if hit.Parent == player.Character then
  64.                         currentTower = portal.Tower.Value
  65.  
  66.                         hit.Parent.HumanoidRootPart.CFrame = towers:FindFirstChild(portal.Tower.Value).SpawnLocation.CFrame + Vector3.new(0, 1, 0)
  67.  
  68.                         timerRunning = true
  69.  
  70.                         currentTowerLabel.TextColor3 = portal.Color
  71.                     end
  72.                 end
  73.             end)
  74.         end
  75.     end)
  76. end
  77.  
  78. function TimerText()
  79.     local milsecs = tostring((timer - math.floor(timer)) * 1000)
  80.     local seconds = tostring(math.floor(timer % 60))
  81.     local minutes = tostring(math.floor(timer / 60))
  82.    
  83.     if #milsecs == 1 then milsecs = "0"..milsecs end
  84.     if #seconds == 1 then seconds = "0"..seconds end
  85.     if #minutes == 1 then minutes = "0"..minutes end
  86.    
  87.     local finalString =
  88.         minutes..":"..seconds.."."..string.sub(milsecs, 1, 2)
  89.    
  90.     return finalString
  91. end
  92.  
  93. function Restart()
  94.     restartKeyHolding = false
  95.     timerRunning = false
  96.  
  97.     player.Character:PivotTo(towers:FindFirstChild(currentTower).SpawnLocation.CFrame * CFrame.new(0, 2.5, 0))
  98.  
  99.     wait()
  100.  
  101.     timerRunning = true
  102.     timer = 0
  103. end
  104.  
  105. runService.RenderStepped:Connect(function(delta)
  106.     if timerRunning then
  107.         timer += delta
  108.     else
  109.         timer = 0
  110.     end
  111.    
  112.     if timerRunning then
  113.         if restartKeyHolding then
  114.             restartHold += delta
  115.  
  116.             if restartHold > restartTime then
  117.                 coroutine.wrap(Restart)()
  118.             end
  119.         else
  120.             restartHold = 0
  121.         end
  122.     else
  123.         restartKeyHolding = false
  124.         restartHold = 0
  125.     end
  126.    
  127.     timerMain.Text = TimerText()
  128.     currentTowerLabel.Text = currentTower
  129.     restartLabel.TextTransparency = 1 - (restartHold / restartTime)
  130. end)
  131.  
  132. getTimerFunction.OnClientInvoke = function()
  133.     return TimerText()
  134. end
  135.  
  136. player.CharacterAdded:Connect(function()
  137.     timerRunning = false
  138.     currentTower = ""
  139.    
  140.     SetupPortals()
  141. end)
  142.  
  143. winpadTouchedEvent.OnClientEvent:Connect(function()
  144.     wait()
  145.     timerRunning = false
  146.     currentTower = ""
  147. end)
  148.  
  149. userInputService.InputBegan:Connect(function(input)
  150.     if input.KeyCode ~= restartKey then return end
  151.     restartKeyHolding = true
  152. end)
  153.  
  154. userInputService.InputEnded:Connect(function(input)
  155.     if input.KeyCode ~= restartKey then return end
  156.     restartKeyHolding = false
  157. end)
  158.  
  159. SetupPortals()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement