. . . Posted December 9, 2021 Share Posted December 9, 2021 (edited) 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 December 10, 2021 by . . . Link to comment https://forums.kleientertainment.com/forums/topic/135936-solved-how-to-get-every-50th-of-a-value/ Share on other sites More sharing options...
IronHunter Posted December 9, 2021 Share Posted December 9, 2021 (edited) You can check if remainder is 0 using the modulo operator. https://www.educba.com/lua-modulo/ 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 December 9, 2021 by IronHunter 1 Link to comment https://forums.kleientertainment.com/forums/topic/135936-solved-how-to-get-every-50th-of-a-value/#findComment-1519822 Share on other sites More sharing options...
CarlZalph Posted December 10, 2021 Share Posted December 10, 2021 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 Link to comment https://forums.kleientertainment.com/forums/topic/135936-solved-how-to-get-every-50th-of-a-value/#findComment-1520223 Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now