ok = game.Players.LocalPlayer:WaitForChild('leaderstats')
okv = ok:WaitForChild('Yeets')
if okv then
script.Parent.Text = okv.Value *5
end
How would I make this script run constantly? Like everytime you get an extra leaderstat value!
3 answers
You can use the Changed event. Like so:
okv.Changed:Connect(function()
script.Parent.Text = okv.Value * 5
end)
Ideally you would be connecting the function to run every time the value of okv changes, using the Changed event. Using that would look like this:
okv.Changed:Connect(function() -- every time okv is changed
script.Parent.Text = okv.Value * 5 -- set the text
)
Alternatively, you could use this method - but not recommended
Loops
There are many types of loops you can use to repeat code.
A for loop is generally used when you want to iterate over a table or if you want to run a piece of code a set amount of times.
A while or repeat until loop is used when you don't know how many times you want to loop but keep looping as long as some condition is true.
If you come to the situation where you want a segment of code to run indefinitely, you can use a while loop and set the condition to be true, so it always runs. However, be sure to use wait() inside the loop so your game doesn't crash/break.
Say you wanted to increment your okv stat by 5 every second. This is how your code would look:
while okv then -- repeat as long as okv exists, you could alternative set it to 'true'
okv = okv + 1 -- add one to the stat
wait(1) -- wait one second between every loop
end
Using loops, you can constantly reassign the script.Parent.Text to be the okv.Value
The issue with loops is that you would be unnecessarily checking for changes if changes do not happen for a period of time. Also, if multiple changes happen very quickly, it would have a sort of 'lag' and only update after a certain period of time rather than exactly when it changes.
Hope this helps.
Changed function is the way to go.
okv.Changed:Connect(function()
script.Parent.Text = okv.Value + x; -- change +x for anything , * 2, / 12, etc
end)
The code above is the ideal way to make the script run whenever the leaderstat changed externally (another script), but if you were to do the entire thing in one script and not have any other object in the game deal with okv you would use a loop such as the following.
while true do
okv = okv + x -- again, x can be whatever
script.Parent.Text = okv.Value
wait(1) -- change 1 to any amount of seconds
end
The way above is a loop you could do, but we can also do the one below.
local okv = script.Parent.okv -- change to the okv destination
while okv do -- this makes sure okv exists instead of constantly running which can let you add extra levels etc using a different leader stats. By means of creating a new leaderstat and okv:remove()
okv = okv + x -- change x
script.Parent.Text = okv.Value
wait(1) -- change 1
end
If you can use the top example (the one using Changed) you should. It saves the server from unnecessary lag by only checking when something has changed. But if the okv score goes up automatically (for a play time counter etc) and is only influenced by 1 object in the game, a loop is more efficient. I hope this helps :)
This discussion is preserved as it was. New answers, comments, and votes are closed.

1 comment
Here is a good article on the wiki: https://wiki.roblox.com/index.php?title=API:Class/IntValue/Changed