How to make a close button make a players health go back to normal?

Hello, I made a script where the players health can go infinite so players can’t kill the player while shopping. The Script does work, but I’m not sure how to make a close button go back to the players normal health. I am also using premium benefits so premium players normal health is 150, instead of the normal health 100.

Open Script:

local player = game:GetService("Players").LocalPlayer
local char = player.Character

script.Parent.MouseButton1Click:Connect(function()
	local frame = script.Parent.Parent.MFrame
	frame.Visible = true
	script.Parent.Visible = false
	
	char:WaitForChild("Humanoid").MaxHealth = math.huge
	char:WaitForChild("Humanoid").Health = math.huge
end)

Close Script:

script.Parent.MouseButton1Click:Connect(function()
	script.Parent.Parent.Visible = false
	script.Parent.Parent.Parent.Shop.Visible = true
end)

please help soon,

Sincerely, papahetfan

Do you want the health go back to what the players health was before he opened the shop or set it to 100 or when premium 150?

Cause players can abuse it and just open the shop when low health.

Just make a value that get’s the humanoids health.

Like, when it gets activated (MouseButton1Click), you should have a variable that gets the Players Max and Normal Health.

local player = game:GetService("Players").LocalPlayer
local char = player.Character
local normHealth
local maxHealth

script.Parent.MouseButton1Click:Connect(function()
	local frame = script.Parent.Parent.MFrame
	frame.Visible = true
	script.Parent.Visible = false
	
	normHealth = char:WaitForChild("Humanoid").Health
	maxHealth = char:WaitForChild("Humanoid").MaxHealth
	wait()
	char:WaitForChild("Humanoid").MaxHealth = math.huge
	char:WaitForChild("Humanoid").Health = math.huge
end)

Now, if the two scripts are in the same script, you can just easily change the humanoid’s health to the now changed health values in the closed script.

Though, if it’s different scripts, you should make an int/number value to be created once clicked. Then change the value of that instance into the initual normal/max health, so that the close script can get that information then delete it.

So, like this:

Open Script
local player = game:GetService("Players").LocalPlayer
local char = player.Character
local normHealth = Instance.new("IntValue")
local maxHealth = Instance.new("IntValue")

normalHealth.Name = "NormalHealth"
normalHealth.Parent = script.Parent

maxHealth.Name = "MaxHealth"
maxHealth.Parent = script.Parent

script.Parent.MouseButton1Click:Connect(function()
	local frame = script.Parent.Parent.MFrame
	frame.Visible = true
	script.Parent.Visible = false
	
	normHealth.Value = char:WaitForChild("Humanoid").Health
	maxHealth.Value = char:WaitForChild("Humanoid").MaxHealth
	wait()
	char:WaitForChild("Humanoid").MaxHealth = math.huge
	char:WaitForChild("Humanoid").Health = math.huge
end)
Close Script
local Player = game.Players.LocalPlayer

local Humanoid = Player.Character:WaitForChild("Humanoid")
local normHealth = script.Parent:WaitForChild("NormalHealth")
local maxHealth = script.Parent:WaitForChild("MaxHealth")

script.Parent.MouseButton1Click:Connect(function()

	if normHealth and maxHealth then

		script.Parent.Parent.Visible = false
		script.Parent.Parent.Parent.Shop.Visible = true

		Humanoid.Health = normHealth.Value
		Humanoid.MaxHealth = maxHealth.Value
	end
end)

Hope this helps! If there’s anything else you need help for, you can either make another post or PM me! Good luck on your game <3

2 Likes

Thank you so much for the help! I am truly hoping I finally release the game sometime 6/4/22

1 Like

I fully wish you the best on your game! If it’s done or you need more help, you can PM me on here.

thank you,I’ll PM you one day.