using this:can someone help me add a Health: 999999999999999999999
to a script.Parent.LightButton.ClickDetector.MouseClick:connect(function()?
script.Parent.LightButton.ClickDetector.MouseClick:Connect(function(player) -- mouseclick event is already emitted with an argument of who clicked the part local character = player.Character -- get the player's character local humanoid = character:FindFirstChild('Humanoid') -- get the character's humanoid if humanoid then humanoid.MaxHealth = math.huge -- make character invincible humanoid.Health = math.huge end end)
QUESTION: How do I make a player invincible from clicking a button?
MouseClick
has a sole parameter. This parameter passes the Player
object of the player who clicked. This is how we can get the Player.
Example below. Assume YOU are the one who clicks the button.
script.Parent.LightButton.ClickDetector.MouseClick:Connect(function(playerWhoClicked) print(playerWhoClicked.Name) -- output: "imaginevreything" end)
Now, in order to change a player's health, we need to access the Humanoid
. Fortunately for us, we can do this via the player we just found from clicking the button. To access the Humanoid, we need to locate the Character. You can do this by running this line of code:
script.Parent.LightButton.ClickDetector.MouseClick:Connect(function(playerWhoClicked) local Character = playerWhoClicked.Character.Humanoid end)
After that, we simply set the MaxHealth
property to X, and Health
to X aswell.
Here's the full code snippet:
script.Parent.LightButton.ClickDetector.MouseClick:Connect(function(playerWhoClicked) local humanoid = playerWhoClicked.Character.Humanoid humanoid .MaxHealth = 999999999999999999999 humanoid .Health = 999999999999999999999 end)
Ending note: connect
is deprecated, so you must use Connect
.