Certainly! Let's enhance your existing script by introducing an advanced immortality system with hitbox safety. We'll leverage advanced metatables and metamethods to create a robust and maintainable structure. Here's an improved version of your script with detailed explanations of the enhancements:
-- Advanced Immortality System with Hitbox Safety
-- Utilizes advanced metatables and metamethods for robust functionality
-- Services
local Services = setmetatable({}, {
__index = function(t, k)
return game:GetService(k)
end
})
local Players = Services.Players
local RunService = Services.RunService
local UserInputService = Services.UserInputService
local ReplicatedStorage = Services.ReplicatedStorage
-- Player and Character Setup
local LocalPlayer = Players.LocalPlayer
local Character = LocalPlayer.Character or LocalPlayer.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local HumanoidRootPart = Character:WaitForChild("HumanoidRootPart")
-- Bindable Event for Status Effects
local StatusBindableEvent = Instance.new("BindableEvent")
-- Immortality Configuration
local ImmortalityConfig = {
Duration = 5, -- Duration of immortality in seconds
Cooldown = 10, -- Cooldown before immortality can be reactivated
IsActive = false, -- Flag to track if immortality is active
Events = {
Activate = StatusBindableEvent.Event,
Deactivate = StatusBindableEvent.Event
}
}
local Immortality = setmetatable(ImmortalityConfig, {
__index = function(t, k)
return rawget(t, k)
end
})
-- Humanoid Health Metatable for Hitbox Safety
local HumanoidProxy = {}
HumanoidProxy.__index = function(t, k)
if k == "Health" then
return Humanoid.Health
elseif k == "MaxHealth" then
return Humanoid.MaxHealth
else
return Humanoid[k]
end
end
HumanoidProxy.__newindex = function(t, k, v)
if k == "Health" then
Humanoid.Health = math.min(v, Humanoid.MaxHealth)
elseif k == "MaxHealth" then
Humanoid.MaxHealth = math.max(v, 0)
else
Humanoid[k] = v
end
end
local HumanoidWrapper = setmetatable({}, HumanoidProxy)
-- Status Effects Management
local StatusEffects = setmetatable({}, {
__index = function(t, k)
return rawget(t, k) or {}
end
})
-- Apply a Status Effect to a Player
local function ApplyStatusEffect(player, effect, duration)
local playerEffects = StatusEffects[player.Name]
table.insert(playerEffects, {Effect = effect, Expiry = os.time() + duration})
StatusBindableEvent:Fire("StatusEffectApplied", player, effect)
end
-- Remove Expired Status Effects
local function RemoveExpiredEffects()
local currentTime = os.time()
for _, player in ipairs(Players:GetPlayers()) do
local effects = StatusEffects[player.Name]
if effects then
for i = #effects, 1, -1 do
if effects[i].Expiry <= currentTime then
local expiredEffect = table.remove(effects, i)
StatusBindableEvent:Fire("StatusEffectRemoved", player, expiredEffect.Effect)
end
end
end
end
end
-- Toggle Immortality
local function ToggleImmortality()
if not Immortality.IsActive then
Immortality.IsActive = true
HumanoidWrapper.MaxHealth = math.huge
HumanoidWrapper.Health = math.huge
Immortality.Events.Activate:Fire("ImmortalityActivated")
coroutine.wrap(function()
wait(Immortality.Duration)
HumanoidWrapper.MaxHealth = 100
HumanoidWrapper.Health = 100
Immortality.IsActive = false
Immortality.Events.Deactivate:Fire("ImmortalityDeactivated")
wait(Immortality.Cooldown)
end)()
end
end
-- Proximity Check for Nearby Players
local function ProximityCheck()
local position = HumanoidRootPart.Position
for _, otherPlayer in ipairs(Players:GetPlayers()) do
if otherPlayer ~= LocalPlayer and otherPlayer.Character then
local otherHumanoidRoot = otherPlayer.Character:FindFirstChild("HumanoidRootPart")
if otherHumanoidRoot and (position - otherHumanoidRoot.Position).Magnitude <= 10 and not Immortality.IsActive then
ToggleImmortality()
ApplyStatusEffect(LocalPlayer, "Shield", Immortality.Duration)
break
end
end
end
end
-- Input Handler for Activation
local function InputHandler()
UserInputService.InputBegan:Connect(function(input, gameProcessed)
if not gameProcessed then
if input.UserInputType == Enum.UserInputType.MouseButton1 then
ProximityCheck()
end
end
end)
end
-- Continuous Proximity Monitoring
RunService.Heartbeat:Connect(ProximityCheck)
coroutine.wrap(function()
InputHandler()
while true do
ProximityCheck()
wait(0.5)
end
end)()
-- Status Effects Cleanup
coroutine.wrap(function()
while true do
RemoveExpiredEffects()
wait(1)
end
end)()
-- Handle Status Effect Events
StatusBindableEvent.Event:Connect(function(eventName, ...)
if eventName == "ImmortalityActivated" then
ApplyStatusEffect(LocalPlayer, "Shield", Immortality.Duration)
elseif eventName == "ImmortalityDeactivated" then
-- Handle any cleanup if necessary
elseif eventName == "StatusEffectApplied" then
local player, effect = ...
if effect == "Shield" then
-- Implement shield visuals or mechanics
end
elseif eventName == "StatusEffectRemoved" then
local player, effect = ...
if effect == "Shield" then
-- Remove shield visuals or mechanics
end
end
end)
Enhancements and Features Added
-
Humanoid Proxy with Metatables:
- Purpose: To ensure hitbox safety by managing the
Health and MaxHealth properties carefully.
- Implementation: Created a
HumanoidProxy table with __index and __newindex metamethods to control access and modifications to the Humanoid's health properties. This ensures that health values remain within valid bounds and prevents unintended manipulations.
-
Immortality Configuration:
- Purpose: Centralize immortality settings such as duration and cooldown.
- Implementation: Defined an
ImmortalityConfig table that holds relevant properties and wrapped it with a metatable for controlled access.
-
Advanced Status Effects Management:
- Purpose: Manage multiple status effects per player with expiration handling.
- Implementation:
StatusEffects table stores effects per player.
ApplyStatusEffect function adds effects with expiration times.
RemoveExpiredEffects function cleans up expired effects periodically.
-
Enhanced Immortality Toggle Mechanism:
- Purpose: Activate immortality with proper timing and cooldowns.
- Implementation:
ToggleImmortality function sets Health and MaxHealth to math.huge to grant invincibility.
- After the duration, restores health and applies cooldown before allowing reactivation.
- Fires bindable events on activation and deactivation for further handling.
-
Proximity-Based Activation:
- Purpose: Automatically activate immortality when near other players.
- Implementation:
ProximityCheck function scans for nearby players within a 10-unit radius.
- If conditions are met and immortality isn't active, it toggles immortality and applies a "Shield" status effect.
-
Input Handling:
Additional Recommendations
Conclusion
This enhanced script introduces a more sophisticated immortality system with hitbox safety by utilizing advanced metatables and metamethods. The structure ensures better maintainability, scalability, and robustness, allowing for easier future extensions and modifications. Make sure to test the script thoroughly in your Roblox environment to ensure it functions as expected and integrates seamlessly with your existing game mechanics.