My problem here seems easy to fix, but I actually have no idea how to.
Basically, I made an Acceleration script, it makes the player gradually increase speed from 5 to 16.
I made it so when the player is Sprinting (by pressing shift), the WalkSpeed will increase from 5 to the amount of speed the player has in the leaderstats, mine is 62.72 as you can see:
So the my speed should go from 5 to 62.72!
the problem is that while when im running without sprinting, and then press sprint, the script won’t stop the process to get to my leaderstats walkspeed, I had to press sprint before running to get to 62.72 walkspeed, here is the clip
robloxapp-20231210-0522573.wmv (419.4 KB)
I had to press shift to sprint before walking to get to my speed in the leaderstats, but i want if the player sprints in the middle of the acceleration process, they will get to their leaderstats speed gradually from the last stop of the acceleration process.
and here is the main line of the script:
if game.ReplicatedStorage[player.Name].IsSprinting.Value == true then
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S or
input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
humanoid.WalkSpeed = 5
local startTime = tick()
while input.UserInputState ~= Enum.UserInputState.End do
local elapsedTime = tick() - startTime
local newSpeed = math.clamp(elapsedTime * accelRate, 5,
player.leaderstats.Speed.Value)
humanoid.WalkSpeed = newSpeed
wait()
end
end
(sorry if my english was bad while writing this)
1 Like
I think there’s some context missing as to what is happening, are you using UserInputService or ContextActionService?
Also instead of the if statement checking 4 parameters you can do if Humanoid.MoveDirection>0 then
there is no context missing, its just the main line because i dont want people saying “bro what is this long script”, anyways heres a wider view:
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local accelRate = player.leaderstats.Speed.Value / 5 -- Speed increase rate (from 0 to 16 in 5 seconds or to the speed of the leaderstats the player has)
local function onKeyPress(input)
if game.ReplicatedStorage[player.Name].IsSprinting.Value == true then
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
humanoid.WalkSpeed = 5
local startTime = tick()
while input.UserInputState ~= Enum.UserInputState.End do
local elapsedTime = tick() - startTime
local newSpeed = math.clamp(elapsedTime * accelRate, 5, player.leaderstats.Speed.Value)
humanoid.WalkSpeed = newSpeed
wait()
end
end
else
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S or input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
humanoid.WalkSpeed = 5
local startTime = tick()
while input.UserInputState ~= Enum.UserInputState.End do
local elapsedTime = tick() - startTime
local newSpeed2 = math.clamp(elapsedTime * accelRate, 5, 16)
humanoid.WalkSpeed = newSpeed2
wait()
end
end
end
end
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
game:GetService("UserInputService").InputEnded:Connect(onKeyPress)
1 Like
Excuse the lack of indentation at some points I’m on mobile
This could be done easier with ContextActionService, but i don’t want to just do a completely different script
The humanoid.MoveDirection
property indicates towards where the player wants to move
And there is no reason to connect to inputEnded since it already stops the speed when player is no longer moving
Tell me if you have any questions or there are errors, it’s hard to code with no IDE
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
local function onKeyPress()
if game.ReplicatedStorage[player.Name].IsSprinting.Value then
if humanoid.MoveDirection>0 then --Player is moving
humanoid.WalkSpeed = 5
local startTime=tick()
while humanoid.MoveDirection>0 do --while player is moving
--All here it's the same
local elapsedTime = tick() - startTime
local newSpeed = math.clamp(elapsedTime * accelRate, 5, player.leaderstats.Speed.Value)
humanoid.WalkSpeed = newSpeed
wait()
end
else --Player is standing still
humanoid.WalkSpeed = 5
local startTime = tick()
end
end
game:GetService("UserInputService").InputBegan:Connect(onKeyPress)
1 Like
im sorry that i didn’t put enough info for this topic, maybe try to view the clip i made (sorry if you had to download it, you can delete it after) and then you will understand what i mean
I can’t reproduce .wmv files on my phone so i can’t watch it
as you can see i had to stop and then press shift and then move so i can get to my 62 walkspeed, i want it to be like you can sprint while moving
Okay i see then
game.ReplicatedStorage[player.Name].IsSprinting:GetPropertyChangedSignal("Value"):Connect(onKeyPress)
Since in the way i wrote it i didn’t requite the input, replacing .InputBegan
for that line should work.
:GetPropertyChangedSignal()
fires and event when the property in the parameters changes, so when the Value changes it will fire and run the function
sorry for being late, but the whole script is not working after replacing the InputBegan
with that line somehow, however I maybe have to make a function
(and name it “SpeedChange”) that will gradually increase the player speed from the last point the shift button was pressed while moving to the leaderstats speed, so we need like if the shift button is pressed then SpeedChange()
. in the middle of the main line of the script:
if game.ReplicatedStorage[player.Name].IsSprinting.Value == true then
if input.KeyCode == Enum.KeyCode.W or input.KeyCode == Enum.KeyCode.S or
input.KeyCode == Enum.KeyCode.A or input.KeyCode == Enum.KeyCode.D then
humanoid.WalkSpeed = 5
local startTime = tick()
while input.UserInputState ~= Enum.UserInputState.End do
local elapsedTime = tick() - startTime
local newSpeed = math.clamp(elapsedTime * accelRate, 5,
player.leaderstats.Speed.Value)
humanoid.WalkSpeed = newSpeed
wait()
end
end
With inputBegan it fires every key press and it’s gonna reset WalkSpeed, on the while use game:GetService("UserInputService"):IsKeyDown(Input.Keycode)
and denounce it so it can’t reset when a new key is pressed.