Leaderstats Problem

I’m trying to make a script that saves the amount of time spent in-game and awards time badges. Before you read this, the script works fine. The problem is it adds to the leaderstats even though you are not in-game.

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")

local badgeservice = game:GetService("BadgeService")
local badgeid = 2124568187 ---- change
local badgeid2 = 2124568189
local badgeid3 = 2124568190
local badgeid4 = 2124568192


game.Players.PlayerAdded:connect(function(plr)
 local leaderstats = Instance.new("Folder", plr)
 leaderstats.Name = "leaderstats"


 local gems = Instance.new("IntValue", leaderstats)
 gems.Name = "Time"


 
 gems.Value = ds1:GetAsync(plr.UserId) or 0
 ds1:SetAsync(plr.UserId, gems.Value)

 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)
 

 end)


game.Players.PlayerAdded:Connect(function(plr)
while true do
wait(1)
plr.leaderstats.Time.Value = plr.leaderstats.Time.Value + 1
		
end
end)




game.Players.PlayerAdded:Connect(function(plr)



while true do

wait(2)
if plr.leaderstats.Time.Value >= 600 then 
			badgeservice:AwardBadge(plr.UserId, badgeid)
			if plr.leaderstats.Time.Value >= 1800 then 
				
				badgeservice:AwardBadge(plr.UserId, badgeid2)
				
				if plr.leaderstats.Time.Value >= 3600 then 
					
					
					badgeservice:AwardBadge(plr.UserId, badgeid3)
					
					if plr.leaderstats.Time.Value >= 7200 then 
						
						badgeservice:AwardBadge(plr.UserId, badgeid4)
						
					end
				
					
				end
				
			end

end
end

end)



Here is the full script, this is the part where I think the problem is.

game.Players.PlayerAdded:Connect(function(plr)

while true do

wait(1)

plr.leaderstats.Time.Value = plr.leaderstats.Time.Value + 1

end

end)

I believe I need to do something that detects if the player leaves the game. Like this.

Players.PlayerRemoving:Connect(function(player)

end)

Please point me the right direction, thanks.

1 Like
 gems.Changed:connect(function()
  ds1:SetAsync(plr.UserId, gems.Value)
 end)

You really shouldn’t be using the data storage that often. If you call it too often, some of your calls will be queued.

Players.PlayerRemoving:Connect(function(player)
ds1:SetAsync(plr.UserId, player.leaderstats.Time.Value)
end)

When a player leaves the game, call the data store to either change their time data, or update it, like what I posted.


Do I change it to something like this or do I make it plr since that’s what is defined. I tried that, but it completely stops the count. @itsLevande

I took your script and re-made it, and it looks good now and it works perfectly:

local datastore = game:GetService("DataStoreService")
local ds1 = datastore:GetDataStore("TimeSave")

local badgeservice = game:GetService("BadgeService")
local badgeid = 2124568187
local badgeid2 = 2124568189
local badgeid3 = 2124568190
local badgeid4 = 2124568192

game.Players.PlayerAdded:Connect(function(plr)
	local leaderstats = Instance.new("Folder")
	leaderstats.Parent = plr
	leaderstats.Name = "leaderstats"


	local gems = Instance.new("IntValue")
	gems.Parent = leaderstats
	gems.Name = "Time"
	
	local data
	
	local success, err = pcall(function()
		data = ds1:GetAsync(plr.UserId)
	end)
	
	if success then
		gems.Value = data
	end 
	
	while true do
		wait(1)
		plr.leaderstats.Time.Value = plr.leaderstats.Time.Value + 1
	end
	
	while true do

		wait(2)
		if plr.leaderstats.Time.Value >= 600 then 
			badgeservice:AwardBadge(plr.UserId, badgeid)
			if plr.leaderstats.Time.Value >= 1800 then 
			
				badgeservice:AwardBadge(plr.UserId, badgeid2)
				
				if plr.leaderstats.Time.Value >= 3600 then 
					
					badgeservice:AwardBadge(plr.UserId, badgeid3)
					
					if plr.leaderstats.Time.Value >= 7200 then 
						
						badgeservice:AwardBadge(plr.UserId, badgeid4)
						
					end
				end
			end
		end
	end	
end)

game.Players.PlayerRemoving:Connect(function(plr)
	ds1:SetAsync(plr.UserId, plr.leaderstats.Time.Value)
end)

game:BindToClose(function()
	
	for _, plr in pairs(game.Players:GetChildren()) do
		
		ds1:SetAsync(plr.UserId, plr.leaderstats.Time.Value)
		
	end
	
end)

If you need help explaining just dm me or ask me here…

I hope i helped and if this works for you (it works for me) make sure to close the thread or mark it solved so people don’t keep commenting

1 Like

Also i know it’s dumb i keep commenting but, i added a game:BindToClose…

It basically makes it so your data saves even in roblox studio, but it will add like extra 1-5 secconds when stopping the game in studio, You can remove it if you want to

1 Like

Thanks. If I want to remove it, do I remove everything under it?

you can just comment the whole thing using group comments

--[[local test1 = "hi"
local test2 = "hi aswell"--]]

The --[[ indicate the opening and the --]] the closing, so just do (whenever you want to):

--[[game:BindToClose(function()
	
	for _, plr in pairs(game.Players:GetChildren()) do
		
		ds1:SetAsync(plr.UserId, plr.leaderstats.Time.Value)
		
	end
	
end)--]]
1 Like

Thanks for the help yesterday, for some reason the script doesn’t award the time badges. Any idea why?

1 Like

Do you own the badges?

I deleted it for the purpose of testing. Someone told me they didn’t get the badge. I tried it and for some reason it didn’t award the badge.

On your game page it says that players have been getting the badge? (like won yesterday etc.)

Yes, that was before the script was remade though. The script it’s self works perfectly, it just for some reason doesn’t award the badges.

1 Like

Idk then,

1 Like