====== Rouding a string ======
==== Finished product ====
{{ :developer_center:developer_editor:script:202408171511_1_.mp4?700 |}}
==== UI Design ====
{{ :developer_center:developer_editor:script:202408171511.mp4?700 |}}
==== Diagram ====
{{:developer_center:developer_editor:script:international_system_of_units_si_..png?nolink|}}
{{:developer_center:developer_editor:script:screenshot_1723893657.png?nolink|}}
====Script====
local name_of_element = nil --- they're a string like : "Coins"
local text_element = nil
local uiid = nil
local trigger_variable = nil --- they're a string
---
function round(e)
while true do
Trigger:wait(0.1)
local result, new_coins_result = VarLib2:getPlayerVarByName(e.eventobjid, 3, trigger_variable )
if new_coins_result >= 1000 and new_coins_result < 1000000 then
new_re = string.format("%.1f", new_coins_result / 1000) .. "K"
elseif new_coins_result >= 1000000 and new_coins_result < 1000000000 then
new_re = string.format("%.0f", new_coins_result / 1000000) .. "M"
elseif new_coins_result >= 1000000000 and new_coins_result < 1000000000000 then
new_re = string.format("%.0f", new_coins_result / 1000000000) .. "B"
elseif new_coins_result >= 1000000000000 and new_coins_result < 1000000000000000 then
new_re = string.format("%.0f", new_coins_result / 1000000000000) .. "T"
---- Continue for "Qa" and "Q"
else
new_re = tostring(new_coins_result)
end
Customui:setText(e.eventobjid, uiid, text_element, name_of_element .. new_re)
end
end
ScriptSupportEvent:registerEvent("UI.Show", round)
==== Explanation ====
=== Variable Initialization: ===
local name_of_element = nil --- they're a string
local text_element = nil
local uiid = nil
local trigger_variable = nil --- they're a string
...
=== Getting the variable from variable library: ===
local result, new_coins_result = VarLib2:getPlayerVarByName(e.eventobjid, 3, trigger_variable )
* **VarLib2:getPlayerVarByName** is used to retrieve a player's variable from variable library.
* See more here: **[[developer_center:developer_editor:script:varlib|Varlib]]**
===Convert it into a new string following the International System of Units (SI) ===
if new_coins_result >= 1000 and new_coins_result < 1000000 then
new_re = string.format("%.1f", new_coins_result / 1000) .. "K"
elseif new_coins_result >= 1000000 and new_coins_result < 1000000000 then
new_re = string.format("%.0f", new_coins_result / 1000000) .. "M"
elseif new_coins_result >= 1000000000 and new_coins_result < 1000000000000 then
new_re = string.format("%.0f", new_coins_result / 1000000000) .. "B"
elseif new_coins_result >= 1000000000000 and new_coins_result < 1000000000000000 then
new_re = string.format("%.0f", new_coins_result / 1000000000000) .. "T"
else
new_re = tostring(new_coins_result)
end
* This section formats the new_coins_result value based on its magnitude. Depending on the range in which **new_coins_result** falls, it formats the number with appropriate suffixes like "K" (thousands), "M" (millions), "B" (billions), "T" (trillions), etc.
===Updating UI ===
Customui:setText(e.eventobjid, uiid, text_element, name_of_element .. new_re)
**Customui:setText** updates the text in the UI, combining **name_of_element** with the formatted **new_re**.
===Event Registration: ===
ScriptSupportEvent:registerEvent("UI.Show", round)
The **round** function is registered as a handler for the "UI.Show" (you can use "Game.AnyPlayer.EnterGame) event, meaning it will be called whenever the "UI.Show" event is triggered.
----
If you have any questions about Developer Tools, feel free to join our official discord server
[[https://discord.gg/NVRZHBChBt|Mini World Global DEV discord]]