How would I speed up the character though Gui getting pressed

As The Title says, I’m wanting to speed Up the character through gui getting pressed, Here’s my script.

local button = script.parent
local function StartPressed()
	Blur.Enabled = false
    --Walkspeed to 16 code here
end
button.mouseButton1Clicked:connect(StartPressed)

Thank you, all answers help.

So just let me ensure I understand what your asking,

You want it so that when a player press a button and hold the button it will increase there speed?

1 Like

no, It’s a start button and it turns the speed from 0 to 16.
edit: for the start menu

1 Like

Then would you not just detect when the button is clicked and change the walk speed of the humanoid to 16 (walkspeed is a propertie of humanoid)?

image

1 Like

but what is the code for finding the player’s humanoid?

1 Like

Well as you are detecting when a button is clicked I am guessing that your doing this in a client script currently (a local script). To get the character you can just get the player by getting the localplayer which is part of the player service (or the game.Players) and then from that you get the character from the player and then the humanoid from the character.

For example:

local Players = game:GetService("Players")
local button = script.parent

local function StartPressed()
	local Plr = Players.LocalPlayer
	local Char = Plr.Character
	local Humanoid = Char:FindFirstChild("Humanoid")
end

button.mouseButton1Clicked:connect(StartPressed)
3 Likes

You need to use Humanoid.Walkspeed
Here is example for your code:

local players = game:GetService("Players")
local player = players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local button = script.parent
local function StartPressed()
 Blur.Enabled = false
 humanoid.WalkSpeed = 16
end
button.mouseButton1Clicked:connect(StartPressed)
1 Like

@LifeDigger is correct with his response but you may want to assure “Char” isn’t nil, you can either just do
if not Char then return end
here’s an example of that:

local Players = game:GetService("Players")
local button = script.parent

local function StartPressed()
	local Plr = Players.LocalPlayer
	local Char = Plr.Character
	if not Char then return end
	local Humanoid = Char:FindFirstChild("Humanoid")
end

button.mouseButton1Clicked:connect(StartPressed)

Though, using that line will make none of the code after that run, so maybe you may want to put all code involving/using “Char” inside of an if statement like this: if Char then
or alternatively you can update the char value every time their character is added like this:

local plr = game.Players.LocalPlayer
plr.CharacterAdded:Connect(function(character)
	char = character
end)
2 Likes

local Character = Player.Character or Player.CharacterAdded:Wait()
Does the job

1 Like

There is a very very low chance that character would equals to nil because your getting it from a player which is in-game but yes if you want to be super safe you could check it is not nil (there is not a massive point to it though). The only reason why it would be nil is if the user left the server super super quickly which it unlikely.

1 Like

In this case player.Character may return nil if the Character hasn’t spawned in yet for some reason, maybe they click the UI button while respawning.

Also player.CharacterAdded:Wait() would yield the script until they respawn, so if they click the button they’d need to reset/die for the script to continue.

1 Like

When respawning theres a period in time that your character does not exist but the UI would still be on screen, not to say it’s that big of a deal but just doing a little check never hurt anyone, plus it’s good practice.

1 Like

I agree, Thank you everyone, that’s just what i needed.

3 Likes

You are right it would yield until they respawn but usually the character loads very fast - I guess it depends on how the developer wants to make the button.

Option 1 like you said: if not char then return end - The button wont do anything and you have to click it multiple times

Option 2: CharacterAdded:Wait() - Would wait until the character loaded but the button would do its job without clicking it again

Both are good ways
But in most cases the character would load fast and you just have to click one time without waiting or anything

1 Like

Option 2 wouldn’t work for regularly clicking the button though, since the character would have to be reloaded first whether that’s through the player dying or resetting.

Also the character doesn’t load as fast as you may think, or at least in my experience it doesn’t, and my point was people may click the button when they die in order to be faster when they spawn.

2 Likes

What do you mean with “character would have to be reloaded first”.

The variable is on top of the script and is made when the script starts there is no need to reload the character first.

Thats why you use: Player.Character or Player.CharacterAdded:Wait()

1 Like

Oh I see what you mean, there’s still a flaw though.

So if I’m right, the method you’re talking about is this

local Players = game:GetService("Players")
local button = script.parent

local Char = game.Players.LocalPlayer.CharacterAdded:Wait()

local function StartPressed()
	local Plr = Players.LocalPlayer
	local Humanoid = Char:FindFirstChild("Humanoid")
end

but if you did that then if the player’s character respawns either by them resetting/dying or by a script calling player:LoadCharacter() then char would no longer point to the character, it would point to nil.

But if you called this in the function then the character would have to be reloaded by them resetting/dying/script calling LoadCharacter(), so that doesn’t work either.

I don’t know why my brain didn’t think of this sooner, but in the function when they do

local Char = plr.Character

they could just do this instead:

local Char  = plr.Character or plr.CharacterAdded:Wait()
4 Likes

Yes exactly xD
If you are just using CharacterAdded:Wait() you would need to reset.
Thats why you have to use Player.Character or Player.CharacterAdded:Wait()

3 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.