I am making a game where if you click people, they will die after a bit. Main script is made but, I wanna know, how do I know which player has the most time in-game? I have made a leaderstats for it.
01 | local Players = game:GetService( "Players" ) |
02 |
03 | local function leaderboard(player) |
04 | local leaderstats = Instance.new( "Folder" , player) |
05 | leaderstats.Name = "leaderstats" |
06 |
07 | local timeD = Instance.new( "IntValue" , leaderstats) |
08 | timeD.Name = "Time" |
09 | timeD.Value = 0 |
10 | while true do |
11 | wait( 1 ) |
12 | timeD.Value = timeD.Value + 1 |
13 | end |
14 | end |
15 |
16 | Players.PlayerAdded:Connect(leaderboard) |
Please help.
01 | local topPlayer = nil --establish topplayer variable |
02 |
03 | local function checkTop() |
04 | local topValue = 0 --set the initial top value to 0 |
05 | for key,plr in pairs (game.Players:GetPlayers()) --getplayers() returns a table. For i,v loop goes through this table |
06 | local leaderstats = plr:FindFIrstChild( "leaderstats" ) |
07 | if leaderstats then --check if the player has a leaderstats folder |
08 | local timeD = leaderstats:FindFirstChild( "timeD" ) |
09 | if timeD.Value > topValue then --see if the timeD value is greater than the top value |
10 | topValue = timeD.Value --if true then set the top value to the timed value |
11 | topPlayer = plr --and the top player to player |
12 | end |
13 | end |
14 | end |
15 | end |
16 |
17 | checkTop() --write this whenever you want to check who's the top player |
Pretty self-explanatory. Let me know if you have any issues.