# (Solved) How to get every 50th of a value

## Recommended Posts

Hi i need help because i suck at lua and math

basically i want to make my code do something when a certain inst.value goes to 50 or 100 or 150 (basically every increment of 50 triggers the code) but i have no idea how to do this. i looked at the math library for lua it goes way over my head

Edited by . . .
##### Share on other sites

You can check if remainder is 0 using the modulo operator.

Spoiler

E.g.

if inst.value % 50 == 0 then

As for automatically checking it getting updated, I am not sure of a easy way to do that. I know components have code to automatically check if data updates to call a function.

The usual method is probably to check whenever your code is relevant and attempts to run. Either via listenforevent, periodictasks, componentupdates, as part of a function call.

If somebody else knows more that would be helpful.

Edited by IronHunter
• 1
##### Share on other sites

15 hours ago, IronHunter said:

As for automatically checking it getting updated, I am not sure of a easy way to do that.

If there's no specific function that updates it and polling with a timer isn't suitable, then there's the metatable proxy hook wherein __newindex + __index is used to forbid a key being set onto a table as it sets it onto another hidden one by proxy, and uses this hidden table to retrieve the value back out.  Would let it run functions whenever the value changes.

```a = {}
a_proxy = {}
a_mt = {
__index = function(t, k)
if k == "power"
then
return rawget(a_proxy, k)
end
return rawget(t, k)
end,
__newindex = function(t, k, v)
if k == "power"
then
print("New power:", v, v % 50 == 0)
return rawset(a_proxy, k, v)
end
return rawset(t, k, v)
end,
}
setmetatable(a, a_mt)
a.power = 40
a.power = a.power + 10```

• 2
• 1