#WalkSpeed Tween

1 messages · Page 1 of 1 (latest)

quartz fern
#

I want to make it so that the player gets faster the longer they press the keys but it doesnt seem to be working. Ive only just started scripting so I might not understand your solution...

local userInput = game:GetService("UserInputService")
local players = game:GetService("Players")
local tweenS = game:GetService("TweenService")

local maxSpeed = 30 
local initialSpeed = 16 

local player = players.LocalPlayer
local WKey = Enum.KeyCode.W
local SKey = Enum.KeyCode.S
local AKey = Enum.KeyCode.A
local DKey = Enum.KeyCode.D
local keyboard = Enum.UserInputType.Keyboard
local humanoid = player.Character.Humanoid

local tween1 = tweenS:Create(humanoid, TweenInfo.new(5), {WalkSpeed = maxSpeed})
local tween2 = tweenS:Create(humanoid, TweenInfo.new(5), {WalkSpeed = initialSpeed})

local function beginWalk(input, gameProcessed)
    if not gameProcessed then        
        if input.UserInputType == keyboard then
            local keycode = input.KeyCode
            if keycode == WKey or keycode == SKey or AKey or keycode == DKey then 
                tween2:Cancel()
                tween1:Play()
            end
        end
    end
end

local function endWalk(input, gameProcessed)
    if not gameProcessed then
        if input.UserInputType == keyboard then
            local keycode = input.KeyCode
            if keycode == WKey or keycode == SKey or AKey or keycode == DKey then
                tween1:Cancel()
                tween2:Play()
            end
        end
    end
end

userInput.InputBegan:Connect(beginWalk)
userInput.InputEnded:Connect(endWalk)
rotund mulch
#

you put "or AKey" instead of "or keycode == AKey"

#

in the beginWalk function

#

and in the endWalk function

quartz fern
#

oh

#

okay wait

rotund mulch
#

if you do "if AKey then" it will always return true because you check if it exists

quartz fern
#

it still doesnt get faster

#

I should put this in StarterPlayerScripts right?

rotund mulch
#

where is the script rn

quartz fern
#

StaerterPlayerScripts

rotund mulch
#

do you get errors?

quartz fern
#

i did it right, but it doesnt get faste

#

19:51:53.357 Players.HannazRolynn.PlayerScripts.PlayerMovement:14: attempt to index nil with 'Humanoid' - Client - PlayerMovement:14

rotund mulch
#

that way it loads when a new character loads

quartz fern
#

okay

#

its working now

quartz fern
rotund mulch
#

np

quartz fern
#

Isnt there like reverse for tween?

#

so I dont need 2 tweens?

quartz fern
# rotund mulch np

and umm sometimes even when I press wasd, the speed doesnt increase and when I release the wasd, the speed doesnt decrease. I need to press and release again a few times before the speed changes

#

I checked with

while true  do
    task.wait(1)
    print(humanoid.WalkSpeed)
end
#

@rotund mulch pls hyelp

rotund mulch
#

back

#

i think the issue is bc you check for all of the keys at once

quartz fern
#

so I need elseif?

rotund mulch
#

idk how good this is but you could create a table like:

local Keys = {
  w = false,
  a = false,
  s = false,
  d = false
}
#

at the start

#

then increase the speed

#

when a key is pressed you set its value to true

#

when its unpressed you set the value to false

#

you keep increasing the speed while at least one of them is true

#

and decrease it when all are false

quartz fern
#

mhmm

#

umm thanks ill try it later gtg rn

quartz fern
clever mural
quartz fern
clever mural
#

You have overlapping behaviour, you try to both start and cancel your tweens. When someone runs in circles spamming wasd they would constantly trigger both the start and ending of your tweens.

quartz fern
#

so I cancel both of them first before playing one of them?

quartz fern
clever mural
quartz fern
#

I do

#

thats why I wanted to just use one tween... or will that not work

clever mural
# quartz fern I do

Tip, when someone asks a question like that answer fully. "Yea, the problem is x, y, z." That way both sides can know if they are aligned or correct miscommunication.

quartz fern
#

my bad

clever mural
#

You need a check to ensure only one of the two happen at a time

rotund mulch
#

you could use runservice to constantly increase the speed if any key is pressed. if none is pressed you decrease it. you can use math.max and math.min to stop at your limits

quartz fern
#

this is gonna take a while... wait a sec

rotund mulch
# quartz fern this is gonna take a while... wait a sec
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")

local Keys = {}
local SpeedToChange = 0.1
local MaxSpeed = 30
local CurrentSpeed = Humanoid.WalkSpeed

UIS.InputBegan:Connect(function(Input, GameProcessed)
    if GameProcessed then return end

    local Keycode = Input.KeyCode
    if Keycode == Enum.KeyCode.W or Keycode == Enum.KeyCode.A or Keycode == Enum.KeyCode.S or Keycode == Enum.KeyCode.D then
        table.insert(Keys, Keycode)
    end
end)

UIS.InputEnded:Connect(function(Input, GameProcessed)
    if GameProcessed then return end

    local Keycode = Input.KeyCode
    if Keycode == Enum.KeyCode.W or Keycode == Enum.KeyCode.A or Keycode == Enum.KeyCode.S or Keycode == Enum.KeyCode.D then
        for i, v in Keys do
            if v == Keycode then
                table.remove(Keys, i)
                break
            end
        end
    end
end)

RunService.Heartbeat:Connect(function()
    if #Keys ~= 0 then
        Humanoid.WalkSpeed = math.min(Humanoid.WalkSpeed + SpeedToChange, MaxSpeed)
    else
        Humanoid.WalkSpeed = math.max(Humanoid.WalkSpeed - SpeedToChange, CurrentSpeed)
    end
end)
quartz fern
#

oh... thank you

languid shoalBOT
#

studio** You are now Level 5! **studio

quartz fern
rotund mulch
#

i will explain the code

#
local UIS = game:GetService("UserInputService")
local RunService = game:GetService("RunService")

local Character = script.Parent
local Humanoid = Character:WaitForChild("Humanoid")
``` this just gets userinputservice, runservice, character and humanoid
#
local Keys = {}
local SpeedToChange = 0.1
local MaxSpeed = 30
local CurrentSpeed = Humanoid.WalkSpeed
``` this creates a table to store every movement key that you are clicking, gets the speed change (like how fast your speed increases or decreases), maxspeed and currentspeed (the minimum speed or your normal walking speed)
#
UIS.InputBegan:Connect(function(Input, GameProcessed)
    if GameProcessed then return end

    local Keycode = Input.KeyCode
    if Keycode == Enum.KeyCode.W or Keycode == Enum.KeyCode.A or Keycode == Enum.KeyCode.S or Keycode == Enum.KeyCode.D then
        table.insert(Keys, Keycode)
    end
end)
``` This stores all of your keys. If you press w, a ,s or d the movement key is saved in the table from above
#

the Keys = {} one

#
UIS.InputEnded:Connect(function(Input, GameProcessed)
    if GameProcessed then return end

    local Keycode = Input.KeyCode
    if Keycode == Enum.KeyCode.W or Keycode == Enum.KeyCode.A or Keycode == Enum.KeyCode.S or Keycode == Enum.KeyCode.D then
        for i, v in Keys do
            if v == Keycode then
                table.remove(Keys, i)
                break
            end
        end
    end
end)
``` This removes the keys from the table if you stop holding them
#
for i, v in Keys do
    if v == Keycode then
        table.remove(Keys, i)
        break
    end
end
``` Used this for loop to remove the key at the right index
#

v is the key stored in Keys

#

if v is the key we want to remove

#

we use its index (i)

#

and remove it from the keys table

#

then break the loop so we dont do more useless checks since we already removed the key we wanted to remove

#
RunService.Heartbeat:Connect(function()
    if #Keys ~= 0 then
        Humanoid.WalkSpeed = math.min(Humanoid.WalkSpeed + SpeedToChange, MaxSpeed)
    else
        Humanoid.WalkSpeed = math.max(Humanoid.WalkSpeed - SpeedToChange, CurrentSpeed)
    end
end)
``` This runs every frame. If there are more than 0 keys `#Keys ~= 0` (#Keys means how many things are in the keys table) the speed of the humanoid is higher. Else, the speed gets smaller
#

math.min makes sure that you dont go above the speed cap

#

it picks the smallest value

#

so if the new speed is higher than the max speed you just keep the max speed

#

no more

#

and math.max does the same but in reverse

#

picks the biggest number, making sure you wont go under your smallest speed

quartz fern
rotund mulch
#

you could make a check at the end but still inside runservice.heartbeat to check if the walkspeed is equal to maxspeed

#

if it is then play the run animation

#

if its not

#

use the normal walk animation

#

i dont really do a lot with animations so i cant help much here

quartz fern
#

aighty