Problem with wait

I’m trying to make an activity tracker script for a ROBLOX group that is linked to a webhook, which puts out this message:

image

As you can see here, it works for 0 minutes, but my problem is making it work for any value above 0 after waiting for 60 seconds. I’ve put my code down below:

local Webhook = "" -- Webhook was removed for privacy reasons
local HttpService = game:GetService("HttpService")

local GroupId = 6101649
local MinRank = 13

game.Players.PlayerAdded:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >=  MinRank then
		local Timer  = Instance.new("NumberValue",Player) Timer.Name = "PlayTime"

		while wait(60) do
			Timer.Value += 1
		end


	end
end)

game.Players.PlayerRemoving:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinRank then
		local payload = HttpService:JSONEncode({
			content = "<:konditori:817494014215323698> **NEW ACTIVITY LOG** \n **Username:** "..Player.Name.." \n **Rank:** "..Player:GetRoleInGroup(GroupId).."  \n **Game:** Konditori Bakery \n **Time:** " ..Player.PlayTime.Value.." minute(s)",
			username = "Activity Logger"
		})

		HttpService:PostAsync(Webhook, payload)
	end
end)

After a minute passes and someone leaves the game (in this case, me), the webhook is no longer fired. I’ve tested the wait to add to the timer with 5 seconds instead of 60 seconds, and that worked, but 60 seconds doesn’t seem to work. I’ve tried to change 60 into 30 * 2 and 10 * 6, but those don’t seem to be working either.

If you have any solutions to this, it would be greatly appreciated.

Do a for loop:

For i=0, 60, 1 do
Timer.Value += 1
wait(1)
end
1 Like

I’ll try that and let you know if it works.

This still doesn’t seem to be working. Any other suggestions?

Can you explain what you’re end goal is? I’m kinda confused on what you are trying to do.

1 Like

I want the playtime value to go up every minute, and if the player is the appropriate rank and the player leaves the game, the 0 in the Discord message changes to the playtime value. This works for the values underneath a minute, and it comes out as 0 minutes. Once it changes to over a minute, it stops working.

Do this in a different script in ServerScriptService:


while true do

For i, v in pairs(game.Players:GetPlayers()) do
v.Timer.Value += 1
end

wait(1)

end

In the current script, do:

game.Players.PlayerRemoving:Connect(function(Player)
	if Player:GetRankInGroup(GroupId) >= MinRank then
local Time = Player.Timer.Value / 60
local RoundedTime = math.floor(Time + 0.5)
		local payload = HttpService:JSONEncode({
			content = "<:konditori:817494014215323698> **NEW ACTIVITY LOG** \n **Username:** "..Player.Name.." \n **Rank:** "..Player:GetRoleInGroup(GroupId).."  \n **Game:** Konditori Bakery \n **Time:** " ..RoundedTime.." minute(s)",
			username = "Activity Logger"
		})

		HttpService:PostAsync(Webhook, payload)
	end
end)

Make sure the Timer name is correct!!!

1 Like

I’m not that good at scripting but I think this could help.

while true do
wait(60)
     Timer.Value += 1
2 Likes

You can get the time the player joined and then use the difference of time when they leave:

local PlayerJoinedTable = {}

Players.PlayerAdded:Connect(function(Player)
    PlayerJoinedTable[Player].JoinTime = os.time() -- os.time() because it's recorded every second, os.clock() is more accurate but we want seconds
end)

Players.PlayerRemoved:Connect(function(Player)
    local ElapsedTime = os.time() - PlayerJoinedTable[Player].JoinTime -- Getting the amount of time spent in the server
    PlayerJoinedTable[Player] = nil -- Setting to nil so it doesn't allocate memory
end)
1 Like

This seems to have broken the webhook, although that’s probably an error on my side. How would you implement this into the script?

Just add the variables to your script. So add the PlayerJoiendTable and the other variables as well: