-- Получаем сервис для работы с тегами
local CollectionService = game:GetService("CollectionService")
-- сервис игроков
local Players = game:GetService("Players")
-- список всех объектов с тегом "Checkpoint"
local checkpoints = CollectionService:GetTagged("Checkpoint")
-- создаем функцию
local function setCheckpoint(checkpart , otherPart)
-- Проверяем, есть ли у объекта Humanoid
-- Это гарантирует, что чекпоинт активировал именно игрок
if otherPart.Parent:FindFirstChild("Humanoid") then
local character = otherPart.Parent
-- Определяем игрока
local player = Players:GetPlayerFromCharacter(character)
-- Если игрок найден — меняем точку возрождения
if player then
player.RespawnLocation = checkpart
end
end
end
-- Подключаем обработчик ко всем существующим чекпоинтам
for i , checkpoint in ipairs(checkpoints) do
checkpoint.Touched:Connect(function(otherPart)
setCheckpoint(checkpoint , otherPart)
end)
end
-- Отслеживаем появление новых чекпоинтов
CollectionService:GetInstanceAddedSignal("Checkpoint"):Connect(function(checkpoint)
checkpoint.Touched:Connect(function(otherPart)
setCheckpoint(checkpoint , otherPart)
end)
end)