I am currentely making a system where a player is AFK in one place, and once they leave or the server closes their time afk is sent back to the original place where they will be rewarded. My main issue with this is when I use a loop to search for the table that represents the player, the :PublishAsync() event will not fire.
I have tried using the PublishAsync event before it and it fires fine, is this a studio feature? It’s hard for me to live test as I would need a server instance in the main game and a client in the AFK place.
I’ll give the code below (This is my first time messing w MessagingService, don’t tear into me too hard )
-- Main Place
local rem = game.ReplicatedStorage:WaitForChild("RequestTeleport")
local ts = game:GetService("TeleportService")
local ms = game:GetService("MessagingService")
local HttpService = game:GetService("HttpService")
local topic = "PremiumRewards"
ms:SubscribeAsync(topic, function(message)
local playerName = message[1]
local timeSpent = message[2]
print("Player Name:", playerName)
print("Time Spent:", timeSpent)
end)
-- AFK Place
local ms = game:GetService("MessagingService")
local hs = game:GetService("HttpService")
local topic = "PremiumRewards"
local rewardsTable = {}
game.Players.PlayerAdded:Connect(function(player)
local newTable = {Player = player, TimeSpent = 0}
table.insert(rewardsTable, newTable)
while task.wait(1) do
if player then
for i, tab in rewardsTable do
if tab.Player == player then
tab.TimeSpent += 1
print(tab.TimeSpent)
end
end
end
end
end)
game.Players.PlayerRemoving:Connect(function(player)
local tab
for i, rewardTab in rewardsTable do
if rewardTab.Player == player then
tab = rewardTab
ms:PublishAsync(topic, tab.Player.Name)
print("PublishAsync Fired!")
break
end
end
if tab then
table.remove(rewardsTable, table.find(rewardsTable, tab))
end
end)
Thanks in advance!