Roblox Forum banner
1 - 2 of 2 Posts

·
Premium member
Joined
·
1,940 Posts
Basically I need help on a script where Everyone has inifinite health. But When They Access Something The Health Goes Back To 100 (Or normal health) Help Will be Appreciated!
You can set up a module to disable/enable invincibility and call it from another script. If you call BreakJoints on a character they'll die regardless. Parent the ModuleScript to ServerStorage, and use it with a script in ServerScriptStorage. The usage example is just to see that it does work, you can paste workspace['Your Character Name'].Humanoid.Health -= 10 into your command bar and try to damage the humanoid when they're invincible or vice versa. You only need to use the highlighted lines when you need to change the player's invincibility.

[CODE lang="lua" title="ModuleScript - ServerStorage"]local module = {}

local connections = {}

-- function to disonnect invincibility and clear dictionary
local function ClearConnection(player)
-- couldn't remember if setting dictionary to nil actually cancels the event and i don't have time to check atm, so i threw this conditional/disconnect in to be safe
if connections[player.UserId] then
connections[player.UserId]:Disconnect()
connections[player.UserId] = nil
end
end

-- if a player leaves, remove their connections if they exist
game:GetService('Players').PlayerRemoving:Connect(function(player)
ClearConnection(player)
end)

function module.EnableInvincibility(player)

-- debounce if character doesn't exist or is already invincible
if not player.Character or connections[player.UserId] then return end
local human = player.Character:FindFirstChildOfClass('Humanoid')

-- set hp to max
human.Health = human.MaxHealth

-- if health changes, set it to max before the character dies
connections[player.UserId] = human:GetPropertyChangedSignal('Health'):Connect(function()
human.Health = human.MaxHealth
end)
end

function module.DisableInvincibility(player)
ClearConnection(player)
end

return module
[/CODE]

[CODE lang="lua" title="Usage Example" highlight="1,5,8"]local module = require(game:GetService('ServerStorage'):WaitForChild('ModuleScript')) -- this is how you get the module from another script

while wait(3) do
for _, player in pairs(game:GetService('Players'):GetPlayers()) do
module.EnableInvincibility(player) -- this is how you'd enable invincibility
print(player.Name .. ' is now invincible!')
wait(3)
module.DisableInvincibility(player) -- this is how you'd disable invincibility
print(player.Name .. ' is no longer invincible!')
end
end[/CODE]
 
1 - 2 of 2 Posts
This is an older thread, you may not receive a response, and could be reviving an old thread. Please consider creating a new thread.
Top