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

how to change walkspeed based on leaderboard stat???

Asked by 5 years ago

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

0
This is not a request site sorry :/ Vain_p 78 — 5y
0
Eveyone requests for help, so do you, just help them, would you rather get is comment or something plausible? Ziffixture 6913 — 5y
0
thanks for all the help guys! I finally got it to work thanks to you! itadakimasu_Kurepu 29 — 5y

2 answers

Log in to vote
0
Answered by
Ziffixture 6913 Moderation Voter Community Moderator
5 years ago
Edited 5 years ago

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

0
I know this is a bit bland, if you need me to explain anything better, just ask Ziffixture 6913 — 5y
0
Are you from C++ or Java or some language that does // User#24403 69 — 5y
2
Java, I don't do much framework anymore, but I know a bit of C++ Ziffixture 6913 — 5y
0
i hate it when I post answers and the asker just stays idle and says nothing.. Ziffixture 6913 — 5y
View all comments (8 more)
0
Jesus Christ my man your answer is way better than mine lol Festive_Boyo 7 — 5y
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;) Ziffixture 6913 — 5y
0
If this Answer helped you press the Accept Answer button, help me help you?:) Ziffixture 6913 — 5y
0
Yeah hopefully he does because this answer is everything he needs Festive_Boyo 7 — 5y
0
Thanks:) I upvoted you anyway, Happy new Year! Ziffixture 6913 — 5y
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. Festive_Boyo 7 — 5y
0
Sorry for the late reply but this helped also, thanks! itadakimasu_Kurepu 29 — 5y
0
Happy to help Ziffixture 6913 — 5y
Ad
Log in to vote
1
Answered by 5 years ago
Edited 5 years ago

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.

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 Ziffixture 6913 — 5y
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. RetroGalacticGamer 331 — 5y
0
Thank you. I am a beginner at scripting so I will definitely learn from this :) Festive_Boyo 7 — 5y

Answer this question