Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

how to change walkspeed based on leaderboard stat???

For example if I have a stat named Speed being displayed on the leaderstats, how can i make a script to check it and depending on what value it is, it will add walkspeed to the character. For example at level 1, you get no extra speed, just regular 16, and when you increase a level it multiples is by 0.5 and add itself to become 24

3 comments

0

This is not a request site sorry :/

0

Eveyone requests for help, so do you, just help them, would you rather get is comment or something plausible?

0

thanks for all the help guys! I finally got it to work thanks to you!

2 answers

✓ Accepted answer

First, make sure you're using a LocalScript. You would need to index the instances we're working with such as:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local leaderstats = player:WaitForChild("leaderstats")
local walkSpeedStat = leaderstats:WaitForChild("Speed")
local levelStat = leaderstats:WaitForChild("Level").Value
local humanoid = character:FindFirstChild("Humanoid")
local walkSpeed = humanoid.WalkSpeed

From here we set up a .Changed statement that's intended to fire once levelStat is greater than it was before:

local currentLevel = levelStat --// allows us to keep track of each *level up*

levelStat.Changed:Connect(function()
   if (levelStat > currentLevel) then --// Check if you leveled up
      currentLevel = levelStat --// Updates tracker
      if (humanoid) then
         walkSpeed = (walkSpeed * .5 + walkSpeed) --// Upgrade walkSpeed
      end
   elseif (levelStat < currentLevel) then
      if (humanoid) then
         local difference = math.floor(currentLevel - levelStat)
         walkSpeed = difference
         currentLevel = (levelStat - 1)
      end
   end
   if (currentLevel < 0 or levelStat < 1) then
      levelStat = 1; currentLevel = 0
      walkSpeed = 16
   end
end)

Put it all together and you get:

local player = game:GetService("Players").LocalPlayer
local character = player.Character
local leaderstats = player:WaitForChild("leaderstats")
local walkSpeedStat = leaderstats:WaitForChild("Speed")
local levelStat = leaderstats:WaitForChild("Level").Value
local humanoid = character:FindFirstChild("Humanoid")
local walkSpeed = humanoid.WalkSpeed

local currentLevel = levelStat

levelStat.Changed:Connect(function()
   if (levelStat > currentLevel) then
      currentLevel = levelStat
      if (humanoid) then
         walkSpeed = (walkSpeed * .5 + walkSpeed)
      end
   elseif (levelStat < currentLevel) then
      if (humanoid) then
         local difference = math.floor(currentLevel - levelStat)
         walkSpeed = difference
         currentLevel = (levelStat - 1)
      end
   end
   if (currentLevel < 0 or levelStat < 1) then
      levelStat = 1; currentLevel = 0
      walkSpeed = 16
   end
end)

Hope this helps

12 comments

0

I know this is a bit bland, if you need me to explain anything better, just ask

0

Are you from C++ or Java or some language that does //

2

Java, I don't do much framework anymore, but I know a bit of C++

0

i hate it when I post answers and the asker just stays idle and says nothing..

0

Jesus Christ my man your answer is way better than mine lol

0

Hey, at least you tried, you had a way better idea faster than I did, I took some time to think, It's New Years anyway, a bit tired too;)

0

If this Answer helped you press the Accept Answer button, help me help you?:)

0

Yeah hopefully he does because this answer is everything he needs

0

Thanks:) I upvoted you anyway, Happy new Year!

0

Oh jeez I fell asleep while on the job, laptop on my lap and all. Happy new Year to you as well my dude.

0

Sorry for the late reply but this helped also, thanks!

0

Happy to help

Ok so to do this, I'd first recommend making the first level 0, to make the script easier to handle. I was thinking of something along the lines of this

local Speed = player.Character.Humanoid.WalkSpeed
player.Character.Humanoid.WalkSpeed = Speed+Speed*0.5

Hopefully this answered your question and if it does not work, please say so and I will see if I did something wrong. Also copying and pasting what I wrote down as an example probably will not work, which is intentional because I do not want to give away free scripts.

3 comments

0

That's the point of all this, also it will not work, because it will only happen once, and won't add according to each level up

0

Instead of adding 0.5 of the original speed you could just multiply by 1.5. Just telling you to make your script simpler.

0

Thank you. I am a beginner at scripting so I will definitely learn from this :)

This discussion is preserved as it was. New answers, comments, and votes are closed.