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!
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.