Hello Developers,
I added a NumberValue into the Player, also added a Datastore for it (works fine).
When the Player joins I added a loop that the value returns -1 until its 0. If 0 it goes Back to 10
Here is the Script for this:
game.Players.PlayerAdded:Connect(function(Player)
while true do
repeat
wait(1)
Player:WaitForChild("FreeSkips").Time.Value -= 1
until Player:WaitForChild("FreeSkips").Time.Value == 0
Player:WaitForChild("FreeSkips").Value +=1
Player:WaitForChild("FreeSkips").Time.Value = 10
end
end)
I also added a TextButton in StarterGui. The Text of the TextButton should be the Text of the Value of the NumberValue. I also tried converting the seconds into Hours:Minutes:Seconds, but this function doesnāt works. Is there a way to fix this? Any help would be appreciated!
Hereās the local Script:
local textLabel = script.Parent
local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
local Time = game.Players:WaitForChild("FreeSkips").Time
while true do
task.wait(1)
textLabel.Text = formatTime(Time)
end
local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor(Time/60)%60
local seconds = math.floor(Time) % 60
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
The % is the modulus function, this makes essentially makes values repeat like a clock, hour % 12 for example would go 0,1,2,3,4,5,6,7,8,9,10,11, 0,1,2,3,4,⦠etc. . it sort of repeats each value, hence we have to repeat minutes and seconds from 60 since seconds are 0-59 like minutes, and hours are the final time value we can have as many as those as we want, if you wanted to add a day function you could do:
local function formatTime(Time)
local days = math.floor(Time/(3600*24))
local hours = math.floor(Time / 3600)%24
local minutes = math.floor(Time/60)%60
local seconds = math.floor(Time) % 60
return string.format("%02d:%02d:%02d:%02d", days, hours, minutes, seconds)
end
Also using wait(1) doesnāt always correlate to real time if there is any lag that can cause wait(1) to actually be longer than 1 second.
Try this info as well Keeping Time.
still doesnāt works⦠(The Value is in the Player and the Value of this Value is getting changed by a Server Script so it saves when you leave. The Text should just copy the Value of the Value and convert it into Hours:minutes:Seconds (for example if the value is 68 seconds the text of the button would be 00:01:08)
The value being changed on the server should replicate on the client.
Just curious, is the āTimeā Value meant to be in the Player rather than the players service, since you reference it as game.Players.Time rather than game.Players.SpecificPlayer.Time
local textLabel = script.Parent
local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
local Time = game.Players.LocalPlayer:WaitForChild("FreeSkips").Time.Value
while true do
task.wait(1)
textLabel.Text = formatTime(Time)
end
still doesnāt works⦠thatās strangeā¦
EDIT: Thanks to @nicemike40 and @TheEternalKittenLord for helping me out. I thought I had to add the .Value thing to the Time location, but I had to add it to formatTIme(Time.Value)
Yes, writing a variable as the value of an numbervalue will the set the value to the number, so when the value of the numbervalue changes the variable doesnt change as its pointing to the actual number, not the value of the numbervalue, for example if you have an numbervalue that with value 20
then local a = numbervalue.Value is equivilant to local a = 20, so when value changes a is still just 20.
Script timeout: exhausted allowed execution time
comes in the output when I delete the function task.wait(1). Is there a way to make it so that it directly changes, so you donāt have to wait 1 second?
local textLabel = script.Parent
local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
local Time = game.Players.LocalPlayer:WaitForChild("FreeSkips").Time
while true do
wait()
textLabel.Text = formatTime(Time.Value)
end
idk why but this is working somehow. When I use task.wait(1) it breaks somehowā¦
(Sorry that I quickly went through the scripts, gyazo only allows for 7 seconds per gif so I had to show the timer was functioning without lag)
Server (ServerScriptService):
local Players = game:GetService('Players')
local RunService = game:GetService('RunService')
local Connections = {}
Players.PlayerAdded:Connect(function(plr)
local t = Instance.new('NumberValue')
t.Name = 'Time'
t.Parent = plr
local c = RunService.Heartbeat:Connect(function(dt)
t.Value += dt
end)
Connections[plr.UserId] = c
end)
Players.PlayerRemoving:Connect(function(plr)
Connections[plr.UserId]:Disconnect()
end)
Client (In the textlabel):
local Time = game.Players.LocalPlayer:WaitForChild('Time')
local function formatTime(Time)
local hours = math.floor(Time / 3600)
local minutes = math.floor((Time - hours * 3600)/60)
local seconds = math.floor(Time - 3600 * hours - minutes * 60)
return string.format("%02d:%02d:%02d", hours, minutes, seconds)
end
Time.Changed:Connect(function()
script.Parent.Text = formatTime(Time.Value)
end)
Let me edit the serverscript to reduce lag with all the runservice connections.