Advertisement
Guest User

Speedcart lua

a guest
Mar 31st, 2023
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.92 KB | None | 0 0
  1. speedcartMaxClients = 10
  2. raceOngoing = false
  3. raceEndSchedule = nil
  4. everyoneDiedSchedule = nil
  5.  
  6. if speedcartClients == nil then
  7.     speedcartClients = {}
  8. end
  9.  
  10. function everyoneDiedCheck()
  11.    
  12.     for i=1,speedcartMaxClients,1 do
  13.         if speedcartMaxClients[i] ~= nil then
  14.             if speedcartMaxClients[i]:getPlayer() ~= nil then
  15.                 x,y,z = speedcartMaxClients[i]:getPlayer():getPosition()
  16.                 if y > 15 then
  17.                     everyoneDiedSchedule = schedule(1000,everyoneDiedCheck)
  18.                     return false
  19.                 end
  20.             end
  21.         end
  22.     end
  23.  
  24.     cancel(raceEndSchedule)
  25.     endRace()  
  26.  
  27.     return true
  28. end
  29.  
  30. function getSpeedcartIdx(client)
  31.     clientIdx = -1
  32.  
  33.     for i=1,speedcartMaxClients,1 do
  34.         if speedcartClient[i] == client then
  35.             clientIdx = i
  36.             break
  37.         end
  38.     end
  39.    
  40.     if clientIdx == -1 then
  41.         for i=1,speedcartMaxClients,1 do
  42.             if speedcartClient[i] == nil then
  43.                 speedcartClient[i] = client
  44.                 clientIdx = i
  45.                 break
  46.             end
  47.         end
  48.     end
  49.  
  50.     return clientIdx
  51. end
  52.  
  53. function speedcartSpawnPlayer(client,x,y,z,cancelled)
  54.  
  55.     if raceOngoing then    
  56.         return client,x,y,z,true
  57.     end
  58.  
  59.     idx = getSpeedcartIdx(client)
  60.     spawnBrick = getNamedBrickIdx("speedcartSpawn" .. idx)
  61.     x,y,z = spawnBrick:getPosition()
  62.     y = y + 2
  63.     return client,x,y,z,cancelled
  64. end
  65. registerEventListener("spawnPlayer","speedcartSpawnPlayer")
  66.  
  67. function speedcartClientLeave(client)
  68.     clearClientBricks(client)
  69.  
  70.     idx = getSpeedcartIdx(client)
  71.     if idx ~= -1 then
  72.         speedcartClient[idx] = nil
  73.     end
  74.     return client
  75. end
  76. registerEventListener("clientLeave","speedcartClientLeave")
  77.  
  78. function speedcartClientPlantBrick(client,x,y,z,isSpecial,specialType,width,length,height,cancelled)
  79.     if raceOngoing then
  80.         cancalled = true
  81.     end
  82.  
  83.     return client,x,y,z,isSpecial,specialType,width,length,height,cancelled
  84. end
  85. registerEventListener("clientPlantBrick","speedcartClientPlantBrick")
  86.  
  87. function speedcartClientClickedBrick(client,brick)
  88.     if client:getPlayer() == nil then
  89.         return client,brick
  90.     end
  91.  
  92.     if brick:getName() == "winBrick" then
  93.         bottomPrintAll("[colour='FFFFFF00']" .. client:getName() .. "[colour='FFFFFFFF'] won the race!",7000)
  94.  
  95.         cancel(everyoneDiedSchedule)
  96.         cancel(raceEndSchedule)
  97.         schedule(7000,endRace)  
  98.     end
  99.  
  100.     return client,brick
  101. end
  102. registerEventListener("clientClickedBrick","speedcartClientClickedBrick")
  103.  
  104. function endRace()
  105.     raceOngoing = false
  106.  
  107.     cancel(everyoneDiedSchedule)
  108.  
  109.     gateBricks = getNumNamedBricks("gate")
  110.     --I guess internal indicies still start at 0 instead of 1 like Lua tables do
  111.     for i=0,gateBricks-1,1 do
  112.         brick = getNamedBrickIdx("gate",i)
  113.         i:setColliding(true)
  114.         r,g,b,a = i:getColor()
  115.         i:setColor(r,g,b,1)
  116.     end
  117.  
  118.     clearBuiltVehicles()
  119.  
  120.     for i=1,speedcartMaxClients,1 do
  121.         if speedcartMaxClients[i] ~= nil then
  122.             speedcartMaxClients[i]:spawnPlayer(0,0,0,false)
  123.             -- Spawn player should teleport a client's player if they already have one
  124.             -- Args don't matter they'll be set in speedcartSpawnPlayer
  125.         end
  126.     end
  127. end
  128.  
  129. function startRace()
  130.     raceOngoing = true
  131.  
  132.     gateBricks = getNumNamedBricks("gate")
  133.     for i=0,gateBricks-1,1 do
  134.         brick = getNamedBrickIdx("gate",i)
  135.         i:setColliding(false)
  136.         r,g,b,a = i:getColor()
  137.         i:setColor(r,g,b,0)
  138.     end
  139.  
  140.     for i=1,speedcartMaxClients,1 do
  141.         if speedcartMaxClients[i] ~= nil then
  142.             clearClientBricks(speedcartMaxClients[i])
  143.         end
  144.     end
  145.  
  146.     everyoneDiedSchedule = schedule(1000,everyoneDiedCheck)
  147.     raceEndSchedule = schedule(1000 * 60 * 5,endRace)
  148. end
  149.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement