Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.3 kB
1
Indexable
Never
-- Dictionary to store player spawn points
local playerSpawnPoints = {}

-- Function to set the player's spawn point
local function setSpawnPoint(player)
    if not playerSpawnPoints[player] then
        local spawnLocation = game:GetService("Workspace"):FindFirstChild("SpawnLocation")
        if spawnLocation then
            playerSpawnPoints[player] = spawnLocation.CFrame
        end
    end
end

-- Function to teleport player to their original spawn point
local function teleportToSpawn(player)
    local spawnPoint = playerSpawnPoints[player]
    if spawnPoint then
        player.Character.HumanoidRootPart.CFrame = spawnPoint
    end
end

-- Function to check map bounds and teleport players if needed
local function checkMapBounds(character)
    local mapBounds = game:GetService("Workspace"):FindFirstChild("MapBounds")
    if mapBounds and character and character:IsDescendantOf(workspace) then
        local characterPosition = character.HumanoidRootPart.Position
        if characterPosition.X < mapBounds.Position.X - mapBounds.Size.X/2 or
            characterPosition.X > mapBounds.Position.X + mapBounds.Size.X/2 or
            characterPosition.Z < mapBounds.Position.Z - mapBounds.Size.Z/2 or
            characterPosition.Z > mapBounds.Position.Z + mapBounds.Size.Z/2 then
            teleportToSpawn(character.Parent)
        end
    end
end

-- Event handler for when a player joins the game
game.Players.PlayerAdded:Connect(function(player)
    setSpawnPoint(player)
    player.CharacterAdded:Connect(function(character)
        wait(1) -- Delay teleportation to allow character to load properly
        checkMapBounds(character)
    end)
end)

-- Event handler for when a player respawns
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAdded:Connect(function(character)
        wait(1) -- Delay teleportation to allow character to load properly
        checkMapBounds(character)
    end)
end)

-- Event handler for character movement
game:GetService("RunService").Heartbeat:Connect(function()
    for _, player in ipairs(game.Players:GetPlayers()) do
        local character = player.Character
        if character then
            checkMapBounds(character)
        end
    end
end)