How to have multiple functions a parameters of a function

I have a function in my code that takes 2 functions. I needed to make an workaround with storing the function in a variable, here is the code.

	local roundMessage = function(timeLeft: number)
		local message
		if #AlivePlayers > 1 then
			message = `Round ends in {timeLeft}, {#AlivePlayers} players are alive`
		else
			message = `Round ends in {timeLeft}, {#AlivePlayers} player is alive`
		end
		return message
	end

	SetMessageTimer(roundMessage, 10, function()
		return #AlivePlayers == 0
	end)

Here is the SetMessageTimer function

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvents = ReplicatedStorage.RemoteEvents
local setMessage = remoteEvents.SetMessage

return function(message: (number) -> (string), duration: number, cancelCondition: () -> (boolean))
	for timeLeft = duration, 1, -1 do
		if cancelCondition == nil then
			setMessage:FireAllClients(message(timeLeft))
			task.wait(1)
		elseif cancelCondition() == false then
			setMessage:FireAllClients(message(timeLeft))
			task.wait(1)
		else
			break
		end
	end
end
1 Like

we can store a message function in a variable before passing it to a timer function
something like this should work fine

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvents = ReplicatedStorage.RemoteEvents
local setMessage = remoteEvents.SetMessage

local function SetMessageTimer(message: (number) -> string, duration: number, cancelCondition: () -> boolean)
    for timeLeft = duration, 1, -1 do
        if cancelCondition and cancelCondition() then
            break
        end
        setMessage:FireAllClients(message(timeLeft))
        task.wait(1)
    end
end

local function getRoundMessage(timeLeft: number)
    local playerCount = #AlivePlayers
    local playerText = playerCount == 1 and "player is" or "players are"
    return string.format("Round ends in %d, %d %s alive", timeLeft, playerCount, playerText)
end

SetMessageTimer(getRoundMessage, 10, function()
    return #AlivePlayers == 0
end)
1 Like

I know, that is just what I did. I want the roundMessage function to be one of the parameters not a seperate variable. Your code is still really help full with the string.format.

I placed the roundmessage function as an inline function which will be serving as the first arg in the setmessagetimer

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local remoteEvents = ReplicatedStorage.RemoteEvents
local setMessage = remoteEvents.SetMessage

local function SetMessageTimer(message: (number) -> string, duration: number, cancelCondition: () -> boolean)
    for timeLeft = duration, 1, -1 do
        if cancelCondition and cancelCondition() then
            break
        end
        setMessage:FireAllClients(message(timeLeft))
        task.wait(1)
    end
end

SetMessageTimer(
    function(timeLeft: number)
        local playerCount = #AlivePlayers
        local playerText = playerCount == 1 and "player is" or "players are"
        return string.format("Round ends in %d, %d %s alive", timeLeft, playerCount, playerText)
    end,
    10,
    function()
        return #AlivePlayers == 0
    end
)
1 Like

Thank you for your contribution to my problem.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.