Hi everyone,
I am trying to get a player’s time to show on the global leaderboard - after a certain block has been touched.
I have a working timer in leaderstats and I have set up an ordered data store (I think) - will post my script below. I am just wondering what is the best way to get the time and show it on the leaderboard. (I was thinking touch event?)
I have managed to get this far with the help of other posts and videos about simple leaderboards and global leaderboards but I just can’t seem to get the time to register. There are no errors or warnings in the output. Thank you.
local DataStoreService = game:Get Service("DataStoreService")
local PlayerTime = DataStoreService:GetOrderedDataStore("PlayerTime")
local function updateLeaderboard()
local success, errorMessage = pcall (function()
local Data = PlayerTime:GetSortedAsync(false, 10)
local TimerPage = Data:GetCurrentPage()
for Rank, data in ipairs(TimerPage) do
local userName = game.Players:GetNameFromUserIdAsync(tonumber(data.key))
local Name = userName
local Time = data.value
local IsOnLeaderboard = false
for i, v in pairs(game.Workspace.leaderboard.leaderboardGui.Backboard:Get Children ()) do
if v.Player.Text == Name then
IsOnLeaderboard = true
break
end
end
if Time and IsOnLeaderboard == false then
local newLBFrame = game.ReplicatedStorage:WairForChild("data holder") :Clone()
newLBFrame.Player.Text = Name
newLBFrame.Time.Text = Time
newLBFrame.Rank.Text = "#".. Rank
newLBFrame.Position = UDim2.new(0, 0, newLBFrame.Position.Y.Scale + (.08 * #game.Workspace.leaderboard.leaderboardGui.Backboard:Get Children ()),0)
newLBFrame.Parent = game.Workspace.leaderboard.leaderboardGui.Backboard
end
end
end)
if not success then
print(errorMessage)
end
end
while true do
for _, player in pairs(game.Players:GetPlayers()) do
PlayerTime:SetAsync(player.UserId, player.leaderstats.Time.Value)
end
for _, frame in pairs(game.Workspace.leaderboard.leaderboardGui.Backboard:GetChildren()) do
frame:Destroy()
end
updateLeaderboard()
print("updated")
wait(10)
end```
2 Likes
Hi there!
Are you able to print out the time? Does it print out the correct time? You can try utilize print functions to see where your script gets up to in execution successfully.
Let me know if I can help you any further.
1 Like
@BruceWayne5222 Hi, the script doesn’t seem to do anything yet - I think this is because I have not been able to ‘get’ the finish time. Was wondering if it would be a touch event or something else? My timer is working and is in leaderstats which shows on screen, but how would I tell it to get the time when the block was touched and put it on the leaderboard?
Sorry if this is not clear as I am very new to this and still learning.
Thank you.
Ah I see, the touched event could be a plausible solution to this, in the same script you’d need to add the touched event somewhere, and you would need to get the player from the character of whatever touched it. Once it has been touched, you could do something like this:
The below snippet of code is not really complete code and you will need to fill in some of the blanks, but I think you will definitely get the hang of it, if you need further help, let me know and I will be more than happy to help you further.
game:GetService("Players").PlayerAdded:Connect(function(Player) --this detects when a new player joins and gives us the instance of the player
local StartTime = tick() --this gives us the amount of seconds passed since the 1st of january, 1970 (aka epoch)
game:GetService("Workspace"):WaitForChild("insert end part name here").Touched:Connect(function(Part) --this detects when something touches the end part
if game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent) ~= nil and game:GetService("Players"):GetPlayerFromCharacter(Hit.Parent) == Player then --this detects if whatever has touched the part is a player and if it is it checks if the person that touched it is our player
--the player has touched the end brick and this is now when we should calculate the time it took them
--to complete it
local EndTime = tick() -- this will be more seconds since epoch than StartTime
local TotalTime = math.floor(EndTime - StartTime) --if this returns a negative number, or 0, make it StartTime - EndTime instead of EndTime - StartTime
--add the time to the leaderboard of the specified player
end
end)
end)
Thank you for that
I have done a touch event script already that gives the player a badge. Sorry, I am just unsure how to get the time the person touched the block to register on the leaderboard. Would something like this work?
endPart.parent.Touched:Connect(function(hit)
if hit.parent:FindFirstChild("Humanoid") then
local plr = game.Players:GetPlayerFromCharacter(hit.Parent)
local endTime = Player.leaderstats:FindFirstChild("Timer")
This could work - but it depends on the context and intention in which you want this time.
Do you want the time it took them from joining the game to touching the part? → If so, you would use the endTime variable you provided in this script.
p.s. I am not sure what Player.leaderstats:FindFirstChild("Timer")
is, assuming it is a text label or something along the lines then you have to add .Text
to the end of it
Player.leaderstats:FindFirstChild("Timer")
Is me trying to reference my timer in leaderstats. This is the problem I am having. Can’t seem to get the time when the end part is touched and then for it to be displayed on the leaderboard. I have another touch event which works fine and gives a badge - I am unsure how to get the time from my timer when the block is touched.
Thank you for all your help - I will keep at it and hopefully get it at some point.
Ohhhh I see. My bad. I think I understand a little more now.
Does Player.leaderstats:FindFirstChild("Timer")
exist or is its lack of existence the problem?
If it does exist just it is not giving you the correct reading, it is likely due to the fact that it is returning an instance rather than a number/value. Is Timer
a text label or value? If it is, you can try adding .Text
or .Value
to the end of Player.leaderstats:FindFirstChild("Timer")
and it should work from there.
If its lack of existence is the issue, or it is a problem any further could you provide me with the full script with the changes you have made since you first posted your problem and I will take a further in-depth look at it and hopefully effectively diagnose your issue.
Hi, yeah it does exist and it is not giving me a reading. I will try the . Value/text and get back to you. It might be a few days as I am away but thank you so much for all your help.