I'll pay 100 robux to the solution!

You right, I just don’t understand how y’all can just easily go through this but then again y’all were new at one point too just like me. W mans :sunglasses:

1 Like

oh my god can you stop reposting and actually check your original post? also you don’t need to pay 100 robux just for some unemployed dude to answer your question for fun on a tuesday night.

anyways here’s the script

local currentTime = 0
local startTime = 0
local timerStopped = true
local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = player:WaitForChild("PersonalBest")
local replicatedStorage = game:GetService("ReplicatedStorage")

local function secondsToMinSecMs(seconds)
	local minutes = math.floor(seconds / 60)
	local remainingSeconds = seconds % 60
	local secondsSplit = math.floor(remainingSeconds)
	local millisecondsSplit = math.floor((remainingSeconds - secondsSplit) * 100)

	return string.format("%02i:%02i.%02i", minutes, secondsSplit, millisecondsSplit)
end

local textLabel = script.Parent
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)

personalBest.Changed:Connect(function()
	personalBest.Value = math.clamp(personalBest.Value, 1/math.huge, math.huge)
	-- if number is less than the smallest positive number possible (x <= 0)
	-- then the line above will make number the smallest positive number possible (x ≈ 0, x > 0)
	-- peventing personal best to be negative numbers or exactly 0
	
	textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end)

local function onStartTouched()
	currentTime = 0
	startTime = tick()
	timerStopped = false
end

local function onEndTouched()
	timerStopped = true
	if currentTime < personalBest.Value then
		personalBest.Value = currentTime
		replicatedStorage.BestTime:FireServer(currentTime)
	end
	textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
end

Runservice.RenderStepped:Connect(function()
	if not timerStopped then
		currentTime = tick() - startTime
		textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
	end
end)

workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)

first thing i did is set up a converter that first divides the seconds by 60 seconds, then get the remainder of seconds/60 then the milliseconds then concatenate them together using string.format()

i also removed some things including math.huge since you cant index Value to a number

no need to give me robux i just did this for fun :slight_smile:

5 Likes

I cant lie you funny as hell. :rofl: Preciate the script, lemme go test it.

1 Like

thank you for the compliment, also i made small changes in the code since i noticed a few errors earlier

2 Likes

It didn’t work, it just says label in test mode

Thanks for improving my code. I will have a look and see what I can do better with it next time :slight_smile:

1 Like

hold on lemme test it too rqrqrq

1 Like

In all honesty I might as well just stick with the seconds timer since it can’t be fixed

Take your time, also I sent screenshots of how my gui and replicatedstorage were organized if you wanna scroll up just a little :sunglasses::+1:t4:

1 Like

i fixed it. my mistake was not changing the function name in some lines and something on the converter.

1 Like

Oh, you mind slidin the script so I can test it as well?

here’s the script it’s in the original reply

1 Like

For some reason it doesn’t work. Thank you for doing all you could to help though. Thank all of yall, I really appreciate it. But since I can’t fix the timer I’ll just stick with this one right here, the original script i had since its the only one that works without everything being ruined:

local Runservice = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local personalBest = player:WaitForChild("PersonalBest") or math.huge
local replicatedStorage = game:GetService("ReplicatedStorage")

local textLabel = script.Parent
textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
personalBest.Changed:Connect(function()
	textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
end)

function onStartTouched(hit)
	timerStopped = false
	startTime = tick()
	currentTime = 0
	Runservice.RenderStepped:Connect(function()
		if not timerStopped then
			currentTime = tick() - startTime
			textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
		end
	end)
end

function onEndTouched(hit)
	if not timerStopped then
		timerStopped = true
		if currentTime > 0 and currentTime < personalBest.Value then
			personalBest.Value = currentTime
			replicatedStorage.BestTime:FireServer(currentTime)
		end
		textLabel.Text = "Timer: " .. string.format("%.3f", currentTime) .. "\nPersonal Best: " .. string.format("%.3f", personalBest.Value)
	end
end

workspace:WaitForChild("StartTimer").Touched:Connect(onStartTouched)
workspace:WaitForChild("EndTimer").Touched:Connect(onEndTouched)

Dude… IT WORKS. Thank you so sk so so so so much :sob:

ofc it works, i tested it in my roblox place

also can you please mark answer the original reply and not the “here’s the script it’s in the original reply” thank you

1 Like

Hell yeah I got you! I really appreciate all of y’all for the help man. Im taking a break from working on the game til the first and then I can work on my leaderboard

1 Like

you’re welcome :slight_smile:

This text will be blurred

1 Like

Now we can all rest stress-free! I can’t thank you guys enough man and I apologize if I came off as rude and/or annoying! I’m just here to learn! :rofl:

2 Likes

i finally fixed the bug that another player can change your timer

Server:

local players = game:GetService("Players")

local dataStoreService = game:GetService("DataStoreService")
local dataStore = dataStoreService:GetDataStore("PersonalBest")

local replicatedStorage = game:GetService("ReplicatedStorage")
local bestTime = replicatedStorage:WaitForChild("BestTime")

local function playerAdded(plr)
	local personalBest = Instance.new("NumberValue")
	personalBest.Name = "PersonalBest"
	personalBest.Value = dataStore:GetAsync("Best_" .. plr.UserId) or math.huge
	personalBest.Parent = plr

	local currentTime = Instance.new("NumberValue")
	currentTime.Name = "CurrentTime"
	currentTime.Value = 0
	currentTime.Parent = plr

	local startTime = Instance.new("NumberValue")
	startTime.Name = "StartTime"
	startTime.Value = 0
	startTime.Parent = plr

	local timerStopped = Instance.new("BoolValue")
	timerStopped.Name = "TimerStopped"
	timerStopped.Value = true
	timerStopped.Parent = plr

	personalBest:GetPropertyChangedSignal("Value"):Connect(function()
		personalBest.Value = math.clamp(personalBest.Value, 1/math.huge, math.huge)
	end)

	while true do
		if timerStopped.Value == false then
			currentTime.Value = tick() - startTime.Value
		end
		task.wait()
	end
end

for _, plr in ipairs(players:GetPlayers()) do
	task.spawn(playerAdded, plr)
end

players.PlayerAdded:Connect(playerAdded)

players.PlayerRemoving:Connect(function(plr)
	dataStore:SetAsync("Best_" .. plr.UserId, plr.PersonalBest.Value)
end)

game:BindToClose(function()
	for _, plr in ipairs(players:GetPlayers()) do
		dataStore:SetAsync("Best_" .. plr.UserId, plr.PersonalBest.Value)
	end
end)

local startTimer = workspace.StartTimer
local endTimer = workspace.EndTimer

startTimer.Touched:Connect(function(part)
	local char = part.Parent
	local hum = char:FindFirstChildOfClass("Humanoid")
	local plr = players:GetPlayerFromCharacter(char)

	if char and plr and hum.Health > 0 and plr.TimerStopped.Value == true then
		plr.CurrentTime.Value = 0
		plr.StartTime.Value = tick()
		plr.TimerStopped.Value = false
	end
end)

endTimer.Touched:Connect(function(part)
	local char = part.Parent
	local hum = char:FindFirstChildOfClass("Humanoid")
	local plr = players:GetPlayerFromCharacter(char)

	if char and plr and hum.Health > 0 then
		plr.TimerStopped.Value = true
		if plr.CurrentTime.Value < plr.PersonalBest.Value then
			plr.PersonalBest.Value = plr.CurrentTime.Value
		end
	end
end)

Client:

local Players = game:GetService("Players")
local player = Players.LocalPlayer
local currentTime = player:WaitForChild("CurrentTime")
local personalBest = player:WaitForChild("PersonalBest")
local replicatedStorage = game:GetService("ReplicatedStorage")

local function secondsToMinSecMs(seconds)
	local minutes = math.floor(seconds / 60)
	local remainingSeconds = seconds % 60
	local secondsSplit = math.floor(remainingSeconds)
	local millisecondsSplit = math.floor((remainingSeconds - secondsSplit) * 100)

	return string.format("%02i:%02i.%02i", minutes, secondsSplit, millisecondsSplit)
end

local textLabel = script.Parent
textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime.Value) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)

while true do
	textLabel.Text = "Timer: " .. secondsToMinSecMs(currentTime.Value) .. "\nPersonal Best: " .. secondsToMinSecMs(personalBest.Value)
	task.wait()
end

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