I have this BindableEvent named "AddListener". It's parented to my timing script, which listens for these events and triggers the callback given when the time specified is reached. The signature is AddListener:Fire(id, triggerTime, cb)
. The first parameter is an identifier for the listener so I can remove it later if needed, kind of like with ContextActionService:BindAction. The second parameter is the number of minutes after midnight when the third parameter, the callback, should be executed.
This is the timing script:
local listeners = {} local AddListener = Instance.new("BindableEvent") AddListener.Event:Connect(function (id, triggerTime, cb) listeners[id] = { ["triggerTime"] = triggerTime, ["cb"] = cb } end) AddListener.Parent = script local RemoveListener = Instance.new("BindableEvent") RemoveListener.Event:Connect(function (id) listeners[id] = nil end) RemoveListener.Parent = script min = 0 minRate = 60 -- how many times faster than a minute irl while true do game.Lighting:SetMinutesAfterMidnight(min) local actualTime = wait(60 / minRate) / (60 / minRate) -- corrects for lag for i, v in pairs(listeners) do if v.triggerTime > min and v.triggerTime < min + actualTime then v.cb() print("executed timing listener with id " .. i) -- doesn't print end end min = min + actualTime end
This is the listener, from another script:
local sss = game:GetService("ServerScriptService") local addListener = sss.TimingScript:FindFirstChild("AddListener") or sss.TimingScript:WaitForChild("AddListener") addListener:Fire("test", 60, function () print("hello world!") -- doesn't print end)
Both of these are server scripts. No errors are thrown, but neither of the print statements are ran.
If you want, you can use _G to make a global variable.
local listeners = {} local AddListener = Instance.new("BindableEvent") AddListener.Event:Connect(function(id, triggerTime, cb) listeners[id] = { ["triggerTime"] = triggerTime, ["cb"] = cb } cb() -- Take you sure you called your function. end) local RemoveListener = Instance.new("BindableEvent") RemoveListener.Event:Connect(function (id) listeners[id] = nil end) _G.GlobalVariables = {Added = AddListener, Remove = RemoveListener}
You can wait this variable with this function who return the Global Variable when is created with the other script. It's my personal method and other scripters use that.
local GlobalVariables = (function() repeat wait() if _G.GlobalVariables ~= nil then return _G.GlobalVariables end until _G.GlobalVariables end)() GlobalVariables.Added:Fire("test", 60, function() print("hello world!") end)
You don't have to assign a parent to your BindableEvent, he exist yet.
To any question post it on the commentaries!!