Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How to give player invicibilty and gives a shield? [UPDATE]

I want to know how to give player invicibility to not take damage of enemies or not take damage when touch a brick.

I maked script like this (But without shield)

local part = script.Parent

part.Touched:connect(function(hit)
	local player = game.Players.LocalPlayer
	if player then
		script.Disabled = true
		script.Sound:Play()
		wait()
		player.Character.Humanoid.Health = math.huge
		wait(7)
		player.Character.Humanoid.Health = math.normal
		script.Disabled = false
	end
end)

1 comment

2 answers

While Code's answer is sufficient barring the inaccurate code.

A more versatile method would be to re-add the health that was taken, another method is to constantly set the health but that's unnecessarily resource intensive.

Here's what I mean:

--Begin Configs--
local InvincibleToggle = true --Toggle whether or not they are invincible by default.
--End Configs--

local function DoInvincible(Character, Toggle)
	if Toggle == true and Character:FindFirstChild("Humanoid") then
		Character.Humanoid.HealthChanged:connect(function(health)
			Character.Humanoid.Health = Character.Humanoid.MaxHealth
		end)
	end
end

game.Players.PlayerAdded:connect(function(player)
	player.CharacterAdded:conenct(function(character)
		DoInvincible(character, InvincibleToggle)
	end)
end)

The function allows you to make other non-playable characters invincible as well. Plus you can remove the PlayerAdded section and replace it with your own method of calling the function.

While you Can set a players health to infinite, some scripts will set it to zero thus resulting in instant death even with infinite health.

You might have problems with lag using this method as I haven't tested it yet, but it should serve whatever purpose you require it for to a decent enough degree.

Let me know if this was unhelpful :>

I'm not good at LUA, but maybe this will help:

if player:connect(true)
 then player.Humanoid.MaxHealth = 100000
	 while true do player.Humanoid.Health = 100000
end

It's probably wrong, but the script to run that kind of function would probably be similar to what I wrote.

This discussion is preserved as it was. New answers, comments, and votes are closed.