Player time display not working

Hello there!
I am having trouble with a script that should make a Time Display above the players head, and if the player has a gamepass, go 7x faster (i will code that later), but it instead does nothing. What am I doing wrong?

local MarketplaceService = game:GetService("MarketplaceService")
local Players = game:GetService("Players")
local time = 0
local gamePassID = 13992448  

local function onPlayerAdded(player)

	local hasPass = false

	-- Check if the player already owns the game pass
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
	end)

	-- If there's an error, issue a warning and exit the function
	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return
	end

	if hasPass == true then
		print(player.Name .. " owns x7 pass with ID " .. gamePassID)
		while true do
			wait (1)
			time = time + 7
		end
		-- Assign this player the ability or bonus related to the game pass
		--
	end
	if hasPass == false then
		print(player.Name .."does not own x7 time! ID:" .. gamePassID)
		while false do
		wait (1)
			time = time + 1
			end
	end
end
-----------make nametags------------
local Character = script.Parent
local PlayerTime= time

local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local NameTag = script:WaitForChild("NameDisplay")
local TagName = NameTag:WaitForChild("PlayerName")

Humanoid.NameDisplayDistance = 0

wait()

TagName.Text = ""..PlayerTime""
wait()
local newTag = NameTag:Clone()
newTag.Name = "NameDisplay"
newTag.Parent = Head-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)

Thanks for any help!

1 Like

Simplified

local increment = 1 -- how much the time is gonna increase by

if hasPass then
   print(player.Name .. " owns x7 pass with ID " .. gamePassID)
   increment = 7 -- set the increment

   coroutine.wrap(function() -- wrap this so it doesn't yield
		while true do
			wait (1)
			time += increment -- this is essentially "time = time + increment" but much better
		end
    end)()
else -- you don't need another "if condition" just use "else"
   print(player.Name .."does not own x7 time! ID:" .. gamePassID)

   coroutine.wrap(function() -- same thing with the previous loop
	  while true do
	      wait (1)
		  time += increment
	   end
    end)()
end

Edit: What class is this script and where is it placed?

regular, wrkspace. i need to have more characters so here you g0

I think it also might have something to do with the nametags…?

Ok, the other part of the script wouldn’t work then. It needs to be parented to StarterCharacterScripts not workspace.

-----------make nametags------------
local Character = script.Parent
--local PlayerTime = time -- this doesn't do anything, it'll just set an old value to the variable

local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local NameTag = script:WaitForChild("NameDisplay")
local TagName = NameTag:WaitForChild("PlayerName")

Humanoid.NameDisplayDistance = 0

-- these waits aren't neccessary
--wait()

--TagName.Text = PlayerTime -- this would just set the text one time and never again.
--wait()
local newTag = NameTag:Clone()
newTag.Name = "NameDisplay"
newTag.Parent = Head-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)
local character = player.Character or player.CharacterAdded:Wait() -- get the character
local head = character:WaitForChild("Head") -- character's head
local tag = head:WaitForChild("NameDisplay") -- name tag

coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
            tag.PlayerName.Text = time -- set the time's text
		end
    end)()
else
   print(player.Name .."does not own x7 time! ID:" .. gamePassID)

   coroutine.wrap(function()
	  while true do
	      wait (1)
		  time += increment
          tag.PlayerName.Text = time -- set the time's text
	   end
    end)()


Still isn’t working, here is what I put together.

local MarketplaceService = game:GetService("MarketplaceService")
	local Players = game:GetService("Players")
	local time = 0
	local gamePassID = 13992448  

	local function onPlayerAdded(player)

		local hasPass = false

		-- Check if the player already owns the game pass
		local success, message = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
		end)

		-- If there's an error, issue a warning and exit the function
		if not success then
			warn("Error while checking if player has pass: " .. tostring(message))
			return
		end

	local increment = 1 -- how much the time is gonna increase by

	if hasPass then
		print(player.Name .. " owns x7 pass with ID " .. gamePassID)
		increment = 7 -- set the increment

		coroutine.wrap(function() -- wrap this so it doesn't yield
			while true do
				wait (1)
				time += increment -- this is essentially "time = time + increment" but much better
			end
		end)()
	else -- you don't need another "if condition" just use "else"
		print(player.Name .."does not own x7 time! ID:" .. gamePassID)

		coroutine.wrap(function() -- same thing with the previous loop
			while true do
				wait (1)
				time += increment
			end
		end)()
	end
-----------make nametags------------
local Character = script.Parent
--local PlayerTime = time -- this doesn't do anything, it'll just set an old value to the variable

local Humanoid = Character:WaitForChild("Humanoid")
local Head = Character:WaitForChild("Head")

local NameTag = script:WaitForChild("NameDisplay")
local TagName = NameTag:WaitForChild("PlayerName")

Humanoid.NameDisplayDistance = 0

-- these waits aren't neccessary
--wait()

--TagName.Text = PlayerTime -- this would just set the text one time and never again.
--wait()
local newTag = NameTag:Clone()
newTag.Name = "NameDisplay"
newTag.Parent = Head-- Connect "PlayerAdded" events to the "onPlayerAdded()" function
Players.PlayerAdded:Connect(onPlayerAdded)
local character = player.Character or player.CharacterAdded:Wait() -- get the character
local head = character:WaitForChild("Head") -- character's head
local tag = head:WaitForChild("NameDisplay") -- name tag

coroutine.wrap(function()
	while true do
		wait (1)
		time += increment
		tag.PlayerName.Text = time -- set the time's text
	end
end)()
else
	print(player.Name .."does not own x7 time! ID:" .. gamePassID)

	coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time -- set the time's text
		end
	end)()

image

Add this to the onPlayerAdded function

-----------make nametags------------
	local character = player.Character or player.CharacterAdded:Wait() -- get the character
	local head = character:WaitForChild("Head") -- character's head
	local tag = head:WaitForChild("NameDisplay") -- name tag

	coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time -- set the time's text
		end
	end)()

	coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time -- set the time's text
		end
	end)()

Still does not seem to work.

What does your entire script look like

	local Players = game:GetService("Players")
	local time = 0
	local gamePassID = 13992448  

	local function onPlayerAdded(player)

		local hasPass = false

		-- Check if the player already owns the game pass
		local success, message = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
		end)

		-- If there's an error, issue a warning and exit the function
		if not success then
			warn("Error while checking if player has pass: " .. tostring(message))
			return
		end

	local increment = 1 -- how much the time is gonna increase by

	if hasPass then
		print(player.Name .. " owns x7 pass with ID " .. gamePassID)
		increment = 7 -- set the increment

		coroutine.wrap(function() -- wrap this so it doesn't yield
			while true do
				wait (1)
				time += increment -- this is essentially "time = time + increment" but much better
			end
		end)()
	else -- you don't need another "if condition" just use "else"
		print(player.Name .."does not own x7 time! ID:" .. gamePassID)

		coroutine.wrap(function() -- same thing with the previous loop
			while true do
				wait (1)
				time += increment
			end
		end)()
	end
-----------make nametags------------
	local character = player.Character or player.CharacterAdded:Wait() -- get the character
	local head = character:WaitForChild("Head") -- character's head
	local tag = head:WaitForChild("NameDisplay") -- name tag

	coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time -- set the time's text
		end
	end)()

	coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time -- set the time's text
		end
	end)()```
local time = 0
local gamePassID = 13992448  
local MarketplaceService = game:GetService("MarketplaceService")

local function onPlayerAdded(character)
        local player = game:GetService("Players"):GetPlayerFromCharacter(character)
		local hasPass = false
        local increment = 1
        local head = character:WaitForChild("Head")
	    local tag = head:WaitForChild("NameDisplay")

		-- Check if the player already owns the game pass
		local success, message = pcall(function()
			hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, gamePassID)
		end)

		-- If there's an error, issue a warning and exit the function
		if not success then
			warn("Error while checking if player has pass: " .. tostring(message))
			return
		end

if hasPass then
      increment = 7

      coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time
		end
	end)()
else
	coroutine.wrap(function()
		while true do
			wait (1)
			time += increment
			tag.PlayerName.Text = time
		end
	  end)()
    end
end

onPlayerAdded(script.Parent) -- assuming it's parented to StarterCharacterScripts


This is in, and still no nametag. Are you calling the tag??

I made a typo and fixed it. Add MarketplaceService

local MarketplaceService = game:GetService("MarketplaceService")

also, are you sure you put it under StarterCharacterScripts


Still nothing.

Ok, add this at the top of the function

local head = character:WaitForChild("Head")

local old = script.NameDisplay:Clone()
old.Parent = head

local tag = head:WaitForChild("NameDisplay")

The errors here can be easily fixed, they’re mostly just things I forgot to add

image Working, but I own the X7 pass.

add this after the if hasPass then condition

   increment = 7
if hasPass then
   increment = 7

it’s another thing I forgot to add

Working! How would I add more passes tho?
Could we call functions or something?

You could create a table that holds the game pass id and check if the player has a gamepass from it

local gamepasses = {
    [1000] = function() -- just an example function
       increment += -- whatever number here
    end
}
local success, message = pcall(function()
    local hasPass = false

    for id, func in pairs(gamepasses) do -- loop through the gamepasses table
	    hasPass = MarketplaceService:UserOwnsGamePassAsync(player.UserId, id) -- check if the own a game pass of the current id
        
        if hasPass then
            func() -- call the function to do stuff
        end
    end
end)

-- just example code, you can take it and modify it

I dont understand it very well, could you make an x2, x5, x10 and x100?