I’m trying to make an acceleration system, but I don’t want it to interfere with my slide system. So, what I’m trying to do is making the acceleration script check if the user is not holding down Left Control. If not, they can accelerate. I’m not sure how to go about this, so I need some help. Here is my script.
local humanoid = script.Parent:WaitForChild("Humanoid")
local uis = game:GetService("UserInputService")
humanoid.Running:Connect(function(speed)
if speed > 0 and game.Players.LocalPlayer.Character.Humanoid.WalkSpeed <2 then
wait(1)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 2
wait(0.5)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 2
wait(0.5)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 4
wait(0.5)
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed += 4
else
game.Players.LocalPlayer.Character.Humanoid.WalkSpeed = 16
end
end)
(Yep, it’s really bad scripting. I’m still very much a noob lol.)
Spell out Roblox APIs and use PascalCase for them.
Define Roblox APIs at the top of your script.
Create variables for values and Instances that you will especially
continually use.
Use task.wait() instead of wait().
Create functions for code that will be repeated, if reasonable (creating
a function to add two values together is not reasonable, in most cases:
local function add(a: number, b: number)
return a + b
end
print(add(5, 6)) -- Output: 11
It makes more sense to just type print(5 + 6), and even requires less characers.
Anyway, if you want to check if the player is not holding a key, you can use the UserInputService:IsKeyDown() method. I am assuming that you want to check if the player is holding the key or not if the speed is more than 0, so you’d write something like this:
local Players = game:GetService("Players")
local UserInputService = game:GetService("UserInputService")
local player = Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local INCREASE_DELAY = 0.5
local function addSpeed(amount: number)
-- You don't have to repeat typing this somewhat long line.
humanoid.WalkSpeed += amount
end
humanoid.Running:Connect(function(speed)
if speed == 0 or humanoid.WalkSpeed > 2 then
humanoid.WalkSpeed = 16
return
end
if UserInputService:IsKeyDown(Enum.KeyCode.LeftControl) then
return
end
task.wait(1)
addSpeed(2)
task.wait(INCREASE_DELAY)
addSpeed(2)
task.wait(INCREASE_DELAY)
addSpeed(4)
task.wait(INCREASE_DELAY)
addSpeed(4)
end)
I don’t know why you are using the values 2 then 4, so I didn’t create a variable for that.
If the script is inside of StarterCharacterScripts, you can just access the character with script.Parent instead of Players.LocalPlayer.Character.
Also, if you want to gradually increase or decrease a value, you can use TweenService.