Skip to content

LuaAPI Example

itvnnnd87 edited this page Oct 24, 2026 · 1 revision

Complete example

Gives WCoinC for time spent online, configured in an INI file.

File Includes\OnlineReward\OnlineReward.ini:

[Config]
Minutes = 30
Coin = 10
Text = You received %d WCoinC for playing %d minutes!

File Includes\OnlineReward\OnlineReward.lua:

local cfg = { Minutes = 30, Coin = 10, Text = "" }
local online = {}   -- [aIndex] = { name, minutes }

local function LoadConfig()
    local ini, err = File.ReadIni("Includes\\OnlineReward\\OnlineReward.ini")
    if ini == nil then Log.AddC(1, "[OnlineReward] " .. err) return end

    local c = ini["Config"] or {}
    cfg.Minutes = tonumber(c.Minutes) or 30
    cfg.Coin = tonumber(c.Coin) or 10
    cfg.Text = c.Text or ""
end

function OnlineReward_OnReadScript()
    LoadConfig()
end

function OnlineReward_OnCharacterEntry(aIndex)
    online[aIndex] = { name = Player.GetName(aIndex), minutes = 0 }
end

function OnlineReward_OnCharacterClose(aIndex)
    online[aIndex] = nil
end

local function EveryMinute()
    for aIndex, s in pairs(online) do
        if Player.IsOnline(aIndex) and Player.GetName(aIndex) == s.name then
            s.minutes = s.minutes + 1

            if s.minutes % cfg.Minutes == 0 then
                Coin.Add(aIndex, cfg.Coin, 0, 0)
                Message.Send(aIndex, 1, string.format(cfg.Text, cfg.Coin, s.minutes))
            end
        else
            online[aIndex] = nil
        end
    end
end

Timer.CreateRepeating(60, "OnlineReward_Minute", EveryMinute)
BridgeFunctionAttach("OnReadScript", "OnlineReward_OnReadScript")
BridgeFunctionAttach("OnCharacterEntry", "OnlineReward_OnCharacterEntry")
BridgeFunctionAttach("OnCharacterClose", "OnlineReward_OnCharacterClose")

Add this line at the end of Main.lua:

LoadModule("OnlineReward\\OnlineReward")

Press Reload LuaAPI on the GameServer control panel to run it at once.


Back to Home

Clone this wiki locally