Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do I make a player's walkspeed increase as they walk?

Asked by 5 years ago
Edited 5 years ago

I've been trying to figure out how to make it so while a player is moving/running forwards, every 0.5 seconds their walkspeed will increase by 0.5. I keep on trying to figure out how to do it and when I do think I've got it, I test and it doesn't work. I've tried using the Roblox wiki, and other sites, but I can't find anything remotely similar on HOW to do this. If you can/do respond, can you explain how to do it, in a sort of tutorial fashion, so I can try to understand it more? EDIT: Here's my most recent attempt. The code doesn't give any errors, but it just doesn't change the player's walkspeed.

PlayerSpeed = game.Players.LocalPlayer.Character.Humanoid.WalkSpeed

while true do
    PlayerSpeed = PlayerSpeed + 0.5
    wait(0.5)
    print(PlayerSpeed)
end

EDIT 2: For more info, I'm placing the script in "StarterPack". I'm not sure if that's the right place.

1 answer

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

The problem lies in the fact that you wrongly assume that the PlayerSpeed variable is a reference to the actual property of the Humanoid. If the walk speed of the Humanoid changes, the variables you have defined won't update; only its WalkSpeed property will change. In order to fix this, you can remove the PlayerSpeed variable and only make direct accesses to the WalkSpeed property of the Humanoid. You could also modify the variables to hold the Humanoid object itself.

-- Global variables are bad practice. Put 'local' in front of your variables when defining them. 
local Client = game.Players.LocalPlayer
local Character = Client.Character or Client.CharacterAdded:Wait() 
-- The character does not instantly load. We must compensate for this. 
local Humanoid = Character.Humanoid

while true do
    Humanoid.WalkSpeed = Humanoid.WalkSpeed + 0.5
    wait(0.5)
    print(Humanoid.WalkSpeed)
end

It is important to note the difference between a reference and a value. You can think of a reference as holding an actual memory location whilst a value just holds normal information (for example, a boolean). In Lua, for example, dictionaries are passed by reference.

local t1 = {
    foo = "abc", 
    bar = 123, 
    foobar = true
}

local t2 = {
    foo = "abc", 
    bar = 123, 
    foobar = true
}

local t1reference = t1
--t1 & t1reference are the same dictionary now, but t2 is different 

t1.foo = "Hello world!"
print(t1.foo) --> Hello world!
print(t1reference.foo) --> Hello world!

print(t2.foo) --> abc

The t1 and t1reference variables both hold the same exact dictionary whilst t2 is a different dictionary even though it holds the same values.

However, data like booleans and numbers are passed via value.

local number = 50 
local numberVal = num

number = 25
print(number) --> 25
print(numberValue) --> 50

local bool1 = false
local bool1Val = bool1
bool1 = true
print(bool1) --> true
print(bool1Val) --> false 

Even though numberValue was set to number, both variables have different memory and so changing one value does not change the other value. Same for the boolean values, bool1Val never changed even after updating bool1.

0
#speed simulator 10 CarMaster17 28 — 5y
0
what User#19524 175 — 5y
Ad

Answer this question