Here's a server-side script that detects when a player is within a zone and prints a message. This script uses a Touched event to detect when a player's character comes into contact with the barrier parts.
-- Server-side script
-- Configuration
local barrierFolderName = "Barriers"
local gracePeriod = 1 -- seconds
local selectedMapName = "SelectedMap"
-- Get the selected map
local selectedMap = game.Workspace:WaitForChild(selectedMapName)
-- Get the barrier folder
local barrierFolder = selectedMap:WaitForChild(barrierFolderName)
-- Function to check if a player is within a barrier
local function checkPlayerInBarrier(player)
-- Get the player's character
local character = player.Character
-- Check if the character exists
if character then
-- Get the character's HumanoidRootPart
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
-- Check if the HumanoidRootPart exists
if humanoidRootPart then
-- Check if the HumanoidRootPart is within a barrier
for _, barrier in pairs(barrierFolder:GetChildren()) do
if barrier:IsA("BasePart") and barrier.CanCollide == false then
if (humanoidRootPart.Position - barrier.Position).Magnitude < barrier.Size.X / 2 then
-- Player is within a barrier
print(player.Name .. " is out of bounds")
return true
end
end
end
end
end
-- Player is not within a barrier
print(player.Name .. " is in bounds")
return false
end
-- Function to handle player movement
local function onPlayerMoved(player)
-- Check if the player is within a barrier
local isInBarrier = checkPlayerInBarrier(player)
-- If the player is within a barrier, wait for the grace period before checking again
if isInBarrier then
wait(gracePeriod)
checkPlayerInBarrier(player)
end
end
-- Connect the onPlayerMoved function to the PlayerAdded event
game.Players.PlayerAdded:Connect(function(player)
-- Wait for the player's character to spawn
player.CharacterAdded:Connect(function(character)
-- Connect the onPlayerMoved function to the HumanoidRootPart's Touched event
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
humanoidRootPart.Touched:Connect(function(hit)
-- Check if the hit object is a barrier
if hit.Parent == barrierFolder then
onPlayerMoved(player)
end
end)
end)
end)
-- Connect the onPlayerMoved function to the PlayerRemoving event
game.Players.PlayerRemoving:Connect(function(player)
-- Disconnect the onPlayerMoved function from the HumanoidRootPart's Touched event
if player.Character then
local humanoidRootPart = player.Character:FindFirstChild("HumanoidRootPart")
if humanoidRootPart then
humanoidRootPart.Touched:Disconnect()
end
end
end)
However, this script may not work as expected due to the following reasons:
- The
Touched event may not fire when the player's character is already within a barrier when the script starts.
- The
Touched event may fire multiple times when the player's character is moving within a barrier.
- The script does not account for cases where the player's character is destroyed or recreated (e.g., when the player respawns).
To improve the script, you can use a different approach, such as:
- Using a
RunService.RenderStepped event to continuously check if the player's character is within a barrier.
- Using a
Region3 to define the barrier area and checking if the player's character is within that area.
Here's an updated version of the script that uses a RunService.RenderStepped event:
-- Server-side script
-- Configuration
local barrierFolderName = "Barriers"
local selectedMapName = "SelectedMap"
-- Get the selected map
local selectedMap = game.Workspace:WaitForChild(selectedMapName)
-- Get the barrier folder
local barrierFolder = selectedMap:WaitForChild(barrierFolderName)
-- Function to check if a player is within a barrier
local function checkPlayerInBarrier(player)
-- Get the player's character
local character = player.Character
-- Check if the character exists
if character then
-- Get the character's HumanoidRootPart
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
-- Check if the HumanoidRootPart exists
if humanoidRootPart then
-- Check if the HumanoidRootPart is within a barrier
for _, barrier in pairs(barrierFolder:GetChildren()) do
if barrier:IsA("BasePart") and barrier.CanCollide == false then
if (humanoidRootPart.Position - barrier.Position).Magnitude < barrier.Size.X / 2 then
-- Player is within a barrier
print(player.Name .. " is out of bounds")
return true
end
end
end
end
end
-- Player is not within a barrier
print(player.Name .. " is in bounds")
return false
end
-- Connect the checkPlayerInBarrier function to the RenderStepped event
game:GetService("RunService").RenderStepped:Connect(function()
for _, player in pairs(game.Players:GetPlayers()) do
checkPlayerInBarrier(player)
end
end)
This script continuously checks if each player's character is within a barrier and prints a message accordingly. However, this approach may be less efficient than the previous one, especially if there are many players and barriers in the game.
You can also use a Region3 to define the barrier area and check if the player's character is within that area. Here's an example:
-- Server-side script
-- Configuration
local barrierFolderName = "Barriers"
local selectedMapName = "SelectedMap"
-- Get the selected map
local selectedMap = game.Workspace:WaitForChild(selectedMapName)
-- Get the barrier folder
local barrierFolder = selectedMap:WaitForChild(barrierFolderName)
-- Function to create a Region3 from a barrier
local function createRegion3(barrier)
return Region3.new(barrier.Position - barrier.Size / 2, barrier.Position + barrier.Size / 2)
end
-- Create a table to store the barrier regions
local barrierRegions = {}
-- Populate the barrier regions table
for _, barrier in pairs(barrierFolder:GetChildren()) do
if barrier:IsA("BasePart") and barrier.CanCollide == false then
table.insert(barrierRegions, createRegion3(barrier))
end
end
-- Function to check if a player is within a barrier
local function checkPlayerInBarrier(player)
-- Get the player's character
local character = player.Character
-- Check if the character exists
if character then
-- Get the character's HumanoidRootPart
local humanoidRootPart = character:FindFirstChild("HumanoidRootPart")
-- Check if the HumanoidRootPart exists
if humanoidRootPart then
-- Check if the HumanoidRootPart is within a barrier
for _, region in pairs(barrierRegions) do
if region:Contains(humanoidRootPart.Position) then
-- Player is within a barrier
print(player.Name .. " is out of bounds")
return true
end
end
end
end
-- Player is not within a barrier
print(player.Name .. " is in bounds")
return false
end
-- Connect the checkPlayerInBarrier function to the RenderStepped event
game:GetService("RunService").RenderStepped:Connect(function()
for _, player in pairs(game.Players:GetPlayers()) do
checkPlayerInBarrier(player)
end
end)
This script creates a Region3 for each barrier and checks if the player's character is within any of those regions. This approach may be more efficient than the previous ones, especially if there are many players and barriers in the game.