Advertisement
Cat_in_the_hat

Checkpoint(s) on block touch

Aug 31st, 2024
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.86 KB | None | 0 0
  1. local checkpoints = {ItemType.CLAY_WHITE, ItemType.CLAY, ItemType.WOOL_BLUE}
  2. local checkpointshistory = {}
  3. local lastCheckedPositions = {}
  4.  
  5. local function sendCheckpointMessage(player, message)
  6.     MessageService.sendInfo(player, message)
  7.     SoundService.playSoundForPlayer(player, SoundType.CAITLYN_CONTRACT_ACCEPT, player:getEntity():getPosition())
  8. end
  9.  
  10. local function checkForPlayers()
  11.     for _, blockType in pairs(checkpoints) do
  12.         local blocks = BlockService.getAllBlocks({blockType})
  13.         for _, block in pairs(blocks) do
  14.             local blockPosition = block.position + Vector3.new(0, 3, 0)
  15.             local nearbyPlayers = PlayerService.getNearbyPlayers(blockPosition, 5.4)
  16.             if nearbyPlayers and #nearbyPlayers > 0 then
  17.                 for _, player in pairs(nearbyPlayers) do
  18.                     local found = false
  19.                     for _, history in pairs(checkpointshistory) do
  20.                         if history.playerId == player.userId then
  21.                             found = true
  22.                             if history.position ~= blockPosition then
  23.                                 history.position = blockPosition
  24.                                 sendCheckpointMessage(player, "Checkpoint saved")
  25.                                 if lastCheckedPositions[player.userId] ~= tostring(blockPosition) then
  26.                                     print("Player " .. player.name .. " is near block at position: " .. tostring(blockPosition))
  27.                                     lastCheckedPositions[player.userId] = tostring(blockPosition)
  28.                                 end
  29.                             end
  30.                             break
  31.                         end
  32.                     end
  33.                     if not found then
  34.                         table.insert(checkpointshistory, {playerId = player.userId, position = blockPosition})
  35.                         sendCheckpointMessage(player, "Checkpoint saved")
  36.                         print("Player " .. player.name .. " is near block at position: " .. tostring(blockPosition))
  37.                         lastCheckedPositions[player.userId] = tostring(blockPosition)
  38.                     end
  39.                 end
  40.             end
  41.         end
  42.     end
  43. end
  44.  
  45. Events.EntitySpawn(function(event)
  46.     local entity = event.entity
  47.     local player = entity:getPlayer()
  48.  
  49.     if player then
  50.         for _, history in pairs(checkpointshistory) do
  51.             if history.playerId == player.userId then
  52.                 local blockPosition = history.position
  53.                 local newPosition = blockPosition + Vector3.new(0, 5, 0)
  54.                 entity:setPosition(newPosition)
  55.                 break
  56.             end
  57.         end
  58.         SoundService.playSoundForPlayer(player, SoundType.GOLD_SPIRIT_DAGGER_SLASH, entity:getPosition())
  59.     end
  60. end)
  61.  
  62. while task.wait(0.1) do
  63.     checkForPlayers()
  64. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement