I am a beginner scripter and i need help on changing you hp and speed by clicking a button
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
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)
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)