Advertisement
Guest User

TimerScript

a guest
May 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.10 KB | None | 0 0
  1. local changeTimerEvent = game.ReplicatedStorage:WaitForChild('ChangeTimerEvent')
  2.  
  3. -- Player start timer event that runs character function
  4. function startTimerPlayer(player)
  5.    
  6.     player.CharacterAdded:Connect(startTimer)
  7.    
  8. end
  9. game.Players.PlayerAdded:Connect(startTimerPlayer)
  10.  
  11. function startTimer(character)
  12.    
  13.     -- Variables and the initial event call
  14.     local timer = 0
  15.     local alive = true
  16.     local player = game.Players:GetPlayerFromCharacter(character)
  17.    
  18.     changeTimerEvent:FireClient(player, timer)
  19.    
  20.     -- Function called when character dies
  21.     local function onDeath()
  22.         -- Best time leaderstats value
  23.         local bestTime = player:FindFirstChild('leaderstats'):FindFirstChild('Best Time')
  24.        
  25.         -- If timer is better than best timechange best time
  26.         if timer > bestTime.Value then
  27.             bestTime.Value = timer
  28.         end
  29.        
  30.         -- Reset variables
  31.         timer = 0
  32.         alive = false
  33.     end
  34.     character:WaitForChild('Humanoid').Died:Connect(onDeath)
  35.    
  36.     wait(1)
  37.    
  38.     -- Timer loop that increases while player is alive
  39.     while alive do
  40.         timer = timer + 1
  41.         changeTimerEvent:FireClient(player, timer)
  42.         wait(1)
  43.     end
  44.    
  45. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement