Help with my TextButton code

Well I’m back again and I need some help with my LocalScript in my TextButton. I’m trying to learn how to code and I need some help with this.

Basically I’m trying to make an easy mode Gui. When you click on the TextButton you enable easy mode which makes the humanoid.MaxHealth and humanoid.Health equal to math.huge. You also change the Text of the TextButton as well. Then when you click it again it makes the humanoid.MaxHealth and humanoid.Health equal to 100 again.

Here’s my code (It’s a LocalScript in a TextButton in a ScreenGui)

local button = script.Parent
local humanoid = -- ???

function onButtonClicked()
	
	humanoid.MaxHealth = math.huge
	humanoid.Health = math.huge
	
	button.Text = "Easy Mode: ON"
	
script.Parent.MouseButton1Click:connect(onButtonClicked)
	
end

function OnButtonClicked()
	
	humanoid.MaxHealth = 100
	humanoid.Health = 100
	
	button.Text = "Easymode: OFF"
end

So I want to know how I can get the humanoid so that I can change the MaxHealth and Health when you press the button.

I would also like to know how I can get the code to check if the player has a gamepass so that if you have the gamepass you can enable the easy mode and if you don’t have the gamepass it will prompt you instead.

Last, if I messed up on the current code I have or if something could be changed please include that.

If this sounds confusing or if I need to clarify a little more please let me know. Like I said I’m trying to learn how to code and I’m no scripter. Any help would be cool.

It would probably be better to define the humanoid in your onButtonClicked function, then have an if-statement inside of your onButtonClicked function checking whether or not easy mode is on. If it’s on, disable it. If it’s off, enable it.

local players = game:GetService('Players')
local localPlayer = players.LocalPlayer -- this will be the player object whose client this script is run on (note this is not the physical character that is in the workspace)

local button = script.Parent
local easyMode = false -- if false, easy mode is off, if true, easy mode is on

local function onButtonClicked() -- local variables (includes functions) are actually slightly faster to access but this is usually negligible but it's generally good practice
    local character = localPlayer.Character -- this will be the physical character in the workspace
    if character then -- the player currently has a character
        local humanoid = character:WaitForChild('Humanoid') -- if the humanoid doesn't exist, this will wait until it does
        if easyMode then -- if easy mode is on, we want to turn it off
            humanoid.MaxHealth = 100
            humanoid.Health = 100
            
            button.Text = 'Easy Mode: OFF'
            
            easyMode = false
        else -- easy mode is off, we want to turn it on
            humanoid.MaxHealth = math.huge
            humanoid.Health = math.huge
            
            button.Health = 'Easy Mode: ON'

            easyMode = true
        end
    end
end

-- now, onButtonClick will be defined, however it won't register clicks yet. To do this we need to connect the button's .MouseButton1Click event to the function, so that the function runs every time the button is clicked.
button.MouseButton1Click:Connect(onButtonClicked)

And for gamepasses, there’s a tutorial here:

2 Likes