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
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
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.