Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

My time counter doesn't work with more than one person. Any fix?

So I've made a time counter that would after the player left give the time to a discord webhook and handle it there. Its a serverscript. The script works good with 1 person but it doesn't work at all with 2+ players.

local Http = game:GetService("HttpService")
local DiscordUr = "[REDACTED]"
game.Players.PlayerAdded:Connect(function(plr)
	player = plr
	local lead = Instance.new("Folder")
	lead.Name = "leaderstats"
	lead.Parent = plr
	timeon = Instance.new("IntValue")
	timeon.Name = "Time"
	timeon.Parent = lead
	
end)

local CountingRoutine = coroutine.create(function()
	while wait(1) do
	print(player.leaderstats.Time.Value)
	player.leaderstats.Time.Value += 1

	end
	
end)
coroutine.resume(CountingRoutine)





game.Players.PlayerRemoving:Connect(function(plr)
	
	local timeoff = timeon.Value
	local timecut = timeoff / 60
	print(timecut)
	if timecut >= 1 then
		local encode = Http:JSONEncode({content = timecut,username = plr.Name})
		Http:PostAsync(DiscordUr,encode)
	else
		local encode = Http:JSONEncode({content = timeoff, username = plr.Name})	
		Http:PostAsync(DiscordUr,encode)
	end
	
end)

1 answer

local Http = game:GetService("HttpService")
local DiscordUr = "[REDACTED]"
local Players=game:GetService"Players"
local tab={}
local function onPlayerAdded(player)
	local val=Instance.new("Folder",player)
	val.Name="leaderstats"val=Instance.new("IntValue",val)
	val.Name="Time"tab[player]=val
	local CountingRoutine = coroutine.create(function()
		    local v=tab[player]
	while wait(1)and player.Parent do
			    v.Value=v.Value+1
	end
		end)
coroutine.resume(CountingRoutine)
end
Players.PlayerAdded:Connect(onPlayerAdded)
for _,v in ipairs(Players:GetPlayers())do onPlayerAdded(v)end
Players.PlayerRemoving:Connect(function(player)
	local v=tab[player].Value
	local MinutesSpend=v~=0 and v/60 or 0
	local encode;
	if MinutesSpend>=1 then
	encode=Http:JSONEncode({content = MinutesSpend,username = player.Name,Type="minutes"})
	else
	encode=Http:JSONEncode({content = v,username = player.Name,Type="seconds"})	
	end
	        Http:PostAsync(DiscordUr,encode)
	tab[player]=nil
end)


3 comments

1

the answer is probably right, but you didnt give any response as to what was wrong, or what needed to be fixed.

0

^ hello this is me the guy who asked for the fix. And yea I agree with the comment above / below (I don't know how the comment placement works here)

0

^ Update: When 2 ppl are in the game and one person leave the bot doesn't send anything

This discussion is preserved as it was. New answers, comments, and votes are closed.