Question about Function and RemoteEvent

Below is a code I have right now

local function annoucementEvent(message, second)
	if second == nil then
		second = 1;
	end
	annoucementTextLabel.Text = message
	annoucementTextLabel.Visible = true
	wait(second) //There is a wait time, this is a bit troubling for me
	annoucementTextLabel.Visible = false
end

annoucementRemote.OnClientEvent:Connect(annoucementEvent)

What happens to the client if the Server fires multiple of these events at the same time.
Will the function be called with the latest message, or will the function be called multiple times?
I would like the function to just be called with the latest message, and all the previous function instances will be canceled and not run anymore(so that they won’t just hide the text label and the announcement failed)
Thank you, Dan

1 Like

The function will be called multiple times.

1 Like

Is there a way to cancel the previous attempt and just go with the latest?

So your saying if the event is fired again before the wait period ends, you want it to override?

1 Like

With the way your code is set up, if it were to be called multiple times in a few seconds there could definitely be a flooding issue.

I would set it up a floodcheck like so:

local flood = {} -- messages arriving too soon show up here
local debounce = false 
local function annoucementEvent(message, second)
    if debounce == false then
        debounce = true
	    if second == nil then
		    second = 1;
	    end
	    annoucementTextLabel.Text = message
	    annoucementTextLabel.Visible = true
	    wait(second) //There is a wait time, this is a bit troubling for me
	    annoucementTextLabel.Visible = false
        debounce = false
    else
        table.insert(flood, true) -- doesn't matter what is entered, could be anything
        local place = #flood
        repeat wait() until debounce == false
        -- by the time debounce is false, other messages could flood in.
        -- place marks how old each message is.
        -- using #flood gives each place a hierarchy.
        -- whichever place value is highest will be the next message.
        -- all messages prior will just be emptied from the table.
        if #flood == place then -- only do this for the LATEST entry
            announcementEvent(message, second)
            table.clear(flood) -- empty the table
        end
    end
end

annoucementRemote.OnClientEvent:Connect(annoucementEvent)
1 Like

Yes, that is correct. I don’t want an announcement to appear for 0.5 second

local pastlabel
local function annoucementEvent(message, second)
    if pastlabel then
        pastlabel:Destroy()
        pastlabel = nil
    end
	second = second or 1

    local label = announcmentTextLabel:Clone()
    label.Text = message
    label.Visible = true
    label.Parent = announcmentTextLabel.Parent
    pastlabel = label

	task.wait(second)

    label.Visible = false
end

annoucementRemote.OnClientEvent:Connect(annoucementEvent)
1 Like

Also, this appears to be a serverwide announcment. Remember to filter it, all user input is EVIL.

1 Like

Oh, it’s just a way for me to announce my randomly generated minigames, and your code has the intended effect I wanted. Thank you very much for the warning

1 Like