#MessagingService publishing multiple times

1 messages · Page 1 of 1 (latest)

undone spire
#
MessagingService:SubscribeAsync("Events", function(message)
    local success, data = pcall(function()
        return typeof(message.Data) == "table" and message.Data or HttpService:JSONDecode(message.Data)
    end)
    
    if not success or not data.EventId then
        warn("[Event Subscriber] Invalid message:", message.Data)
        return
    end
    
    if processedEvents[data.EventId] then
        warn("[Event Subscriber] Duplicate event ignored:", data.EventName)
        return
    end
    
    processedEvents[data.EventId] = true
    print("[Event Subscriber] Received event:", data.EventName, "ID:", data.EventId)

    adminEvent(data.EventName)
    
    task.delay(600, function()
        processedEvents[data.EventId] = nil
    end)
end)

This part is where I subscribe

#
elseif action == "Events" then
            print("[AdminAction] Publishing Event:", other)

            local HttpService = game:GetService("HttpService")
            local eventId = HttpService:GenerateGUID(false)
            local eventData = {
                EventName = other,
                EventId = eventId,
                Timestamp = os.time()
            }
            
            local success, err = pcall(function()
                MessagingService:PublishAsync("Events", eventData)
            end)
            if not success then
                warn("Failed to publish Event:", err)
            end

This is where I publish

#

It kind of get published 4-5 times? Maybe?

shell timber
#

Also, mind me asking in what kind of function that button code takes place?

undone spire
#

Hm I cant send it

#

It kind of receieves it like this:
events.AdminAction.OnServerInvoke = function(player, action, other)
local role = player:GetRoleInGroup(35254642):lower()
print(action, other)

if string.find(role, "developer") or string.find(role, "owner") or string.find(role, "holder") then
#

if action == "Events" then
print("[AdminAction] Publishing Event:", other)

        local HttpService = game:GetService("HttpService")
        local eventId = HttpService:GenerateGUID(false)
        local eventData = {
            EventName = other,
            EventId = eventId,
            Timestamp = os.time()
        }
        
        local success, err = pcall(function()
            MessagingService:PublishAsync("Events", eventData)
        end)
        if not success then
            warn("Failed to publish Event:", err)
        end
#

@shell timber nevermind that it only prints once

#

But it seems to be subscribing multiple times

#
MessagingService:SubscribeAsync("Events", function(message)
    local success, data = pcall(function()
        return typeof(message.Data) == "table" and message.Data or HttpService:JSONDecode(message.Data)
    end)
    
    if not success or not data.EventId then
        warn("[Event Subscriber] Invalid message:", message.Data)
        return
    end
    
    if processedEvents[data.EventId] then
        warn("[Event Subscriber] Duplicate event ignored:", data.EventName)
        return
    end
    
    processedEvents[data.EventId] = true
    print("[Event Subscriber] Received event:", data.EventName, "ID:", data.EventId)

    adminEvent(data.EventName)
    
    task.delay(600, function()
        processedEvents[data.EventId] = nil
    end)
end)
shell timber
#

When does it subscribe?

#

(Or well, I can see it in the code of course. But is it just at the start of runtime or is this all inside a module)

undone spire
#

It subscribe the moment I publish it ill send a video if u want

#

This is all inside the same piece of script (serverscriptservice)

shell timber
undone spire
#

serverscript

shell timber
#

So it only subscribes on that first line, which gets ran once. Right?

undone spire
#

Yes but it does the action multiple times

shell timber
#

Let me grab my laptop, may be easier to read on there

undone spire
#

Huh why is it like this

#
MessagingService:SubscribeAsync("Events", function(message)
    local success, data = pcall(function()
        return typeof(message.Data) == "table" and message.Data or HttpService:JSONDecode(message.Data)
    end)
    
    if not success or not data.EventId then
        warn("[Event Subscriber] Invalid message:", message.Data)
        return
    end
    
    if processedEvents[data.EventId] then
        warn("[Event Subscriber] Duplicate event ignored:", data.EventName)
        return
    end
    
    processedEvents[data.EventId] = true
    print("[Event Subscriber] Received event:", data.EventName, "ID:", data.EventId)

    adminEvent(data.EventName)
    
    task.delay(600, function()
        processedEvents[data.EventId] = nil
    end)
end)

Part where I subscribe

#

Idk why I cant send full code

shell timber
#

Uh that video is just the script...

undone spire
#

Oops wrong file

plucky harnessBOT
#

studio** You are now Level 16! **studio

shell timber
#

So, to quickly recap:

print("[Event Subscriber] Received event:", data.EventName, "ID:", data.EventId)

prints multiple times. Yet

print("[AdminAction] Publishing Event:", other)

only prints once,
right?

undone spire
#

Hm

#

It prints [AdminAction] once

#

And it only seems to recieve it once?

shell timber
shell timber
#

fires once, receives once?

undone spire
#

Yep but does the action multiple times

#

This part I think if I havent shown you

local function adminEvent(eventName)
    events.ShowNotification:FireAllClients("Event Triggered: " .. eventName)

    if eventName == "PigFrenzy" then
        local pigFrenzyCounter = 0
        repeat
            pigFrenzyCounter += 1
            spawnPiggy(nil, "Penny")
            task.wait(0.5)
            spawnPiggy(nil, "Georgie")
            task.wait(0.5)
            spawnPiggy(nil, "Mother")
            task.wait(0.5)
            spawnPiggy(nil, "Father")
            task.wait(0.5)
            spawnPiggy(nil, "Skelly")
            task.wait(0.5)
            spawnPiggy(nil, "Teacher")
            task.wait(0.5)
            spawnPiggy(nil, "Sheepy")
            task.wait(0.5)
            spawnPiggy(nil, "Mimi")
            task.wait(0.5)
            spawnPiggy(nil, "Doggy")
            task.wait(0.5)
            spawnPiggy(nil, "Bunny")
            task.wait(0.5)
            spawnPiggy(nil, "Owell")
            task.wait(0.5)
            spawnPiggy(nil, "Robby")
            task.wait(0.5)
        until pigFrenzyCounter == 2

    elseif eventName == "LegendaryParade" then
        local legendaryCounter = 0
        repeat
            spawnPiggy("Legendary", nil)
            legendaryCounter += 1
            task.wait(0.5)
        until legendaryCounter == 5
    end
end
shell timber
undone spire
#

the events.ShowNotification:FireAllClients("Event Triggered: " .. eventName runs multiple times

#

Which I call the function here

MessagingService:SubscribeAsync("Events", function(message)
    local success, data = pcall(function()
        return typeof(message.Data) == "table" and message.Data or HttpService:JSONDecode(message.Data)
    end)
    
    if not success or not data.EventId then
        warn("[Event Subscriber] Invalid message:", message.Data)
        return
    end
    
    if processedEvents[data.EventId] then
        warn("[Event Subscriber] Duplicate event ignored:", data.EventName)
        return
    end
    
    processedEvents[data.EventId] = true
    print("[Event Subscriber] Received event:", data.EventName, "ID:", data.EventId)

    adminEvent(data.EventName)
    
    task.delay(600, function()
        processedEvents[data.EventId] = nil
    end)
end)
#

Sorry if Im sending the same code multiple times

shell timber
undone spire
#

Yep

shell timber
#

But the print above the call only gets printed once?

#

No wait, it printed twice

undone spire
#

No that was earlier testing

#

In the streamable video it was another test which I forgot to record

shell timber
#

So it prints once but gives the noti twice?

undone spire
#

Yeah it prints once and give the notification more than twice but im assuming the adminEvent function is ran multiple times

#

[Event Subscriber] Received event: is ran once

#

But adminEvent is probably ran multiple times

shell timber
#

Just to be sure, mind adding a debug print into the adminEvent func and seeing if that also prints multiple times

#

as this could be caused by an issue with the notification handling

undone spire
#

Oh youre right

#
local function adminEvent(eventName)
    print("Admin Event Triggered")
    events.ShowNotification:FireAllClients("Event Triggered: " .. eventName)

    if eventName == "PigFrenzy" then
        local pigFrenzyCounter = 0
        repeat
            pigFrenzyCounter += 1
            spawnPiggy(nil, "Penny")
            task.wait(0.5)
            spawnPiggy(nil, "Georgie")
            task.wait(0.5)
            spawnPiggy(nil, "Mother")
            task.wait(0.5)
            spawnPiggy(nil, "Father")
            task.wait(0.5)
            spawnPiggy(nil, "Skelly")
            task.wait(0.5)
            spawnPiggy(nil, "Teacher")
            task.wait(0.5)
            spawnPiggy(nil, "Sheepy")
            task.wait(0.5)
            spawnPiggy(nil, "Mimi")
            task.wait(0.5)
            spawnPiggy(nil, "Doggy")
            task.wait(0.5)
            spawnPiggy(nil, "Bunny")
            task.wait(0.5)
            spawnPiggy(nil, "Owell")
            task.wait(0.5)
            spawnPiggy(nil, "Robby")
            task.wait(0.5)
        until pigFrenzyCounter == 2

    elseif eventName == "LegendaryParade" then
        local legendaryCounter = 0
        repeat
            spawnPiggy("Legendary", nil)
            legendaryCounter += 1
            task.wait(0.5)
        until legendaryCounter == 5
    end
end

I added the print "Admin Event Triggered" and it only printed ONCE

#

But the events.ShowNotification:FireAllClients() is fired like 5 times

shell timber
#

Mind showing where you handle that RE?

undone spire
#
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local TweenService = game:GetService("TweenService")

local events = ReplicatedStorage:WaitForChild("Events")

local frame = script.Parent

events.ShowNotification.OnClientEvent:Connect(function(text, level)
    if level == "Cash" then
        script.Purchase:Play()
        return
    end
    local notification = frame.Template:Clone()
    notification.Parent = frame
    notification.Name = text
    notification.Text = text
    notification.UIScale.Scale = 0
    notification.Visible = true
    if level == 2 then
        script.Steal:Play()
        notification.TextColor3 = Color3.fromRGB(255, 0, 0)
    elseif level == 1 then
        script.Error:Play()
        notification.TextColor3 = Color3.fromRGB(255, 0, 0)
    elseif level == 0 then
        script.Normal:Play()
        notification.TextColor3 = Color3.fromRGB(255, 255, 255)
    end
    TweenService:Create(notification.UIScale, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut, 0, false, 0), {Scale = 1}):Play()
    task.wait(4)
    TweenService:Create(notification.UIScale, TweenInfo.new(0.5, Enum.EasingStyle.Exponential, Enum.EasingDirection.InOut, 0, false, 0), {Scale = 0}):Play()
    task.wait(0.5)
    notification:Destroy()
end)

I think its the function problem honestly

undone spire
shell timber
#

Odd, I expected some kind of cache causing the issue to be frank. Mind me asking where this script is located?

shell timber
#

yes

undone spire
shell timber
#

Odd, then it's not some gc issue neither... fucking hell...

undone spire
#

Yeah its fine in roblox studios

#

But it triggers a lot of times in the main game

shell timber
#

Does it print anything differently in the game?

undone spire
#

No not really

#

Its just that block

shell timber
#

So every debug print still only prints once?

#

I mean, the only real thing I can think of is multiple instances of that same local script existing