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)
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)
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)
@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)
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.
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.
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.
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
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.
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()
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()