Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
1

How to change your hp and speed by clicking a button???

Asked by 8 years ago

I am a beginner scripter and i need help on changing you hp and speed by clicking a button

0
I wont tell you how to do this but i will tell you the golden rule were not here to create scrips were here to fix them so put a little effort and come back qwrn12 85 — 8y
0
Please review the ROBLOX Official WIKI, as it may have the answer to your question. :) TheeDeathCaster 2368 — 8y

1 answer

Log in to vote
1
Answered by
Goulstem 8144 Badge of Merit Moderation Voter Administrator Community Moderator
8 years ago

What You Need To Do

So, you want to change your Character's health and speed by a click of a button? This requires deeper knowledge of a few things;

  • The MouseButton1Click Event

    The MouseButton1Click event is a "listener" that fires when the client right clicks their mouse.

  • How Do I Access My Player?

    You can easily access your player by indexing LocalPlayer from Players, this is only accessible in LocalScripts

  • Where Is Everything Located?

    The Health and WalkSpeed attributes of your player are located in the Character's Humanoid as Properties


Elaboration

Now that you know these essentials, we can start by defining the Player, with the method stated above.. as well as what you're going to change the WalkSpeed and Health Properties to.

local plr = game.Players.LocalPlayer
local health = 200
local speed = 50

Simple enough, right? Next step is to define your function, that will fire whenever someone clicks your button. Access your Character's Humanoid, and manipulate the WalkSpeed, Health, and MaxHealth Properties. Note: The Health Property can not be higher than the MaxHealth Property.

--Define the function
function onClick()
    --Define the humanoid
    local hum = plr.Character:FindFirstChild("Humanoid")
    --Manipulate the Properties
    hum.MaxHealth = health
    hum.Health = health
    hum.WalkSpeed = speed
end

Now, the final step is to tie your function to an event. Events are listeners that fire whenever a specific action occurs, such as.. someone clicking your TextButton. The event we're going to use is called the MouseButton1Down event, which fires whenever the client right-clicks down the specified gui.

You can tie function to events using something called a connection statement. A connection statement will consist of 4 things;

  • The Object you're tying the event to

  • The Event you're trying to the object

  • The 'connect' method

  • The function you're wanting to tie to the event

So, if we apply all the credentials to your situation, the data would be;

  • Your TextButton

  • The MouseButton1Down Event

  • The 'connect' method

  • The function above, 'onClick'

script.Parent.MouseButton1Down:connect(onClick)

Code

So, if we put that all together it would look like this;

local plr = game.Players.LocalPlayer
local health = 200
local speed = 50

function onClick()
    local hum = plr.Character:FindFirstChild("Humanoid")
    hum.MaxHealth = health
    hum.Health = health
    hum.WalkSpeed = speed
end

script.Parent.MouseButton1Down:connect(onClick)
0
Epic LetalNightmare 99 — 3y
Ad

Answer this question