I made notes as im learning how to script and have been trying to make a safezone, when I enter the safezone I cannot die and after leaving its radius or no longer being inside of it I still have infinite health
local Safezone = game.Workspace.SafeZone – Defines Safezone/area
local playersInSafeZone = {} – Table to track players inside the safezone/area
local DEFAULT_MAX_HEALTH = 100 – Default max health
– Function to handle when a player enters the safezone/area
local function onPlayerEnter(player)
– Check if said player has a humanoid
if player.Character and player.Character:FindFirstChild(“Humanoid”) then
local humanoid = player.Character.Humanoid
– Set health
humanoid.MaxHealth = math.huge – Infinite Health
humanoid.Health = humanoid.MaxHealth – Sets health to full
table.insert(playersInSafeZone, player)
end
end
– Function to handle when a player leaves the safezone/area
local function onPlayerExit(player)
– Check if the player is in the table
for i, p in ipairs(playersInSafeZone) do
if p == player then
– Restore health to normal
if player.Character and player.Character:FindFirstChild(“Humanoid”) then
local humanoid = player.Character.Humanoid
humanoid.MaxHealth = 100 – Sets max health to default value
humanoid.Health = humanoid.Maxhealth – Restore health
end
table.remove(playersInSafeZone, i) – Removes player from the table
break
end
end
end
– Detect player entering the safezone using touched event
Safezone.Touched:Connect(function(hit)
local player = game:GetService(“Players”):GetPlayerFromCharacter(hit.Parent)
if player then
onPlayerEnter(player)
end
end)
– Detects players leaving the safezone/area
game:GetService(“RunService”).Heartbeat:Connect(function()
for _, player in pairs(playersInSafeZone) do
–Check if the player is still inside the safezone/area
if player.Character and player.Character:FindFirstChild(“HumanoidRootPart”) then
local distance = (player.Character.HumanoidRootPart.Position - Safezone.Position).magnitude
if distance > Safezone.Size.X / 2 then --Player leaving safezone/area
onPlayerExit(player)
end
end
end
end)