Time system for my hangout game

I am trying to make a system that tracks the amount of time played in seconds for my hangout game. I am adding in a VIP gamepass, so whenever you own it, you get 2x time from one of the perks.

local GAME_PASS_ID = 902195340

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")


local function loadPlayerTime(player)
	local success, result = pcall(function()
		return TimeDataStore:GetAsync(tostring(player.UserId))
	end)
	if success then
		if result then
			return result
		else
			return 0
		end
	else
		warn("Failed to load data for player " .. player.Name)
		return 0
	end
end


local function savePlayerTime(player, time)
	local success, errorMessage = pcall(function()
		TimeDataStore:SetAsync(tostring(player.UserId), time)
	end)
	if not success then
		warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
	end
end

local function onPlayerEntered(newPlayer)

	local initialTime = loadPlayerTime(newPlayer)

	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = newPlayer

	local secs = Instance.new("IntValue")
	secs.Name = "Time Played"
	secs.Value = initialTime
	secs.Parent = stats

	local hasGamePass = newPlayer:HasPass(GAME_PASS_ID)


	while newPlayer and newPlayer.Parent do
		wait(1)
		local timeIncrement = 1
		if hasGamePass then
			timeIncrement = 2
		end
		secs.Value = secs.Value + timeIncrement
	end
end

local function onPlayerRemoving(player)
	local timePlayed = player.leaderstats and player.leaderstats["Time Played"]
	if timePlayed then
		savePlayerTime(player, timePlayed.Value)
	end
end

Players.PlayerAdded:Connect(onPlayerEntered)
Players.PlayerRemoving:Connect(onPlayerRemoving)

Is the script I am using, it doesn’t seem to work though any ideas on what to do from here?

Which part os not working? Do you get any errors in the output?

Ill list some problems that I see

  1. Player object doesn’t have a method ‘:HasPass’, so the game can’t tell if player has the gamepass or not
  2. The script is stuck in that while loop in the player added function, so it can’t detect the player removing or anything else

Solutions:
Problem 1:
Use MarketplaceService’s ‘UserOwnsGamepassAsync’ instead

local mps = game:GetService('MarketplaceService')
mps:UserOwnsGamePassAsync(newPlayer.UserId,GAME_PASS_ID)

Problem 2:
Spawn the function to run it on a different core (think of it as creating a new script for the while loop)

spawn(function()
	while newPlayer and newPlayer.Parent do
		task.wait(1)
		local timeIncrement = 1
		if hasGamePass then
			timeIncrement = 2
		end
		secs.Value = secs.Value + timeIncrement
	end
end)

Final Code:

local GAME_PASS_ID = 902195340

local Players = game:GetService("Players")
local DataStoreService = game:GetService("DataStoreService")
local mps = game:GetService('MarketplaceService')
local TimeDataStore = DataStoreService:GetDataStore("TimeDataStore")


local function loadPlayerTime(player)
	local success, result = pcall(function()
		return TimeDataStore:GetAsync(tostring(player.UserId))
	end)
	if success then
		if result then
			return result
		else
			return 0
		end
	else
		warn("Failed to load data for player " .. player.Name)
		return 0
	end
end


local function savePlayerTime(player, time)
	local success, errorMessage = pcall(function()
		TimeDataStore:SetAsync(tostring(player.UserId), time)
	end)
	if not success then
		warn("Failed to save data for player " .. player.Name .. ": " .. errorMessage)
	end
end

local function onPlayerEntered(newPlayer)

	local initialTime = loadPlayerTime(newPlayer)

	local stats = Instance.new("Folder")
	stats.Name = "leaderstats"
	stats.Parent = newPlayer

	local secs = Instance.new("IntValue")
	secs.Name = "Time Played"
	secs.Value = initialTime
	secs.Parent = stats

	local hasGamePass = mps:UserOwnsGamePassAsync(newPlayer.UserId,GAME_PASS_ID)


	spawn(function()
	while newPlayer and newPlayer.Parent do
		task.wait(1)
		local timeIncrement = 1
		if hasGamePass then
			timeIncrement = 2
		end
		secs.Value = secs.Value + timeIncrement
	end
    end)
end

local function onPlayerRemoving(player)
	local timePlayed = player.leaderstats["Time Played"]
	if timePlayed then
		savePlayerTime(player, timePlayed.Value)
	end
end

Players.PlayerAdded:Connect(onPlayerEntered)
Players.PlayerRemoving:Connect(onPlayerRemoving)
1 Like

I get 0 errors in my output its very weird.

Thank you so much that updated script perfectly solved my issue!!!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.