Hey guys. So I made a timer that starts when you touch a part and ends when you touch another (used to know your time on an obby). I need it to be able to display that timer to the textbox (script.Parent.Text). It currently outputs an error and I don’t know why… The error says: “Line 25: Attempt to call a string value”. I tried resolving this without ever succeeding. Please help! Thank you.
local RunService = game:GetService("RunService")
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local connection = nil
workspace.Timer.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
if hit.Parent.Name == player.Name then
if (connection) then return end -- already running, do nothing
local startTime = tick()
-- create a connection that will run every frame until
-- connection is disconnected in the stop method
connection = RunService.Stepped:Connect(function()
local elapsed = tick() - startTime
local milliseconds = math.floor(1000 * math.fmod(elapsed, 1))
local seconds = math.floor(elapsed % 60)
local minutes = math.floor(elapsed / 60)
-- prints out every single frame
script.Parent.Text = (string.format("Elapsed time: %d:%.02d.%.03d", minutes, seconds, milliseconds))
end)
end
end
end)
workspace.Stop.Touched:Connect(function(hit)
if game.Players:GetPlayerFromCharacter(hit.Parent) then
if hit.Parent.Name == player.Name then
print("Stopped")
if (connection) then
connection:Disconnect()
connection = nil
end
end
end
end)