Change time of day from textbox

ServerSript:

script.Parent.RemoteEvent.OnServerEvent:Connect(function(Text)
	game.Lighting.ClockTime = Text
end)

Local Script:

script.Parent.MouseButton1Click:Connect(function()
	local Text = script.Parent.Parent.TextBox.Text
	script.RemoteEvent:FireServer(Text)
end)

For some reason it only works once and it just sets it to 0

I think that the first parameter passed to the function in OnServerEvent is the player that calls it

script.Parent.RemoteEvent.OnServerEvent:Connect(function(Player, Text)
	game.Lighting.ClockTime = Text
end)

Here is the documentation

1 Like

Use a RemoteFunction for this:

function CheckAndChange(player: Player,  AppliedText)
	
	if type(tonumber(AppliedText)) == "number" then
		game.Lighting.ClockTime = AppliedText
	else
		warn("Text Must be a Number!")
		return
	end

end

RemoteFunction.OnServerInvoke = CheckAndChange
script.Parent.MouseButton1Click:Connect(function()
	local Text = script.Parent.Parent.TextBox.Text
	RemoteFunction:InvokeServer(Text)
end)

I think that’s really complicated and there’s a much easier way to do this, only with one remote event:
LocalScript(TextLabel is your timer and remoteevent is a remoteevent inside ReplicatedStorage:)

local remoteevent = game.ReplicatedStorage.TimerEvent
remoteevent.OnClientEvent:Connect(function(min,sec)
if sec < 10 then
script.Parent.TextLabel.Text = min..":0"..sec
else
script.Parent.TextLabel.Text = min..":"..sec
end
if min == 0 and sec == 1 then
script.Parent.TextLabel.Text = "Time's up!"
wait(math.huge)
end
end)

Script in ServerScriptService

local remoteevent = game.ReplicatedStorage.TimerEvent
local b = false
local min = 20
local sec = 59
while b = false do
sec = sec - 1
if sec <= 0 then
min = min - 1
sec = 59
end
if sec == 1 and min == 0 then
remoteevent:FireAllClients(min, sec)
wait(math.huge)
else
remoteevent:FireAllClients(min,sec)
task.wait(1)
end
end

Also this is just something I wrote on my phone so i give no guarentee that this actualky works. If there is any errors please feel free to tell me!

Its a long script I know but its just basic code and it shouldn’t be that hard to locate errors.

this has 1 error

change:

while b = false do

to:

while b == false do

Not really, I dont think its complicated, i just dont think you are understanding the code:

function CheckAndChange(player: Player,  AppliedText)
	
	if type(tonumber(AppliedText)) == "number" then -- Checks if the given text is a number and can be converted to one
		game.Lighting.ClockTime = AppliedText -- if so, it Applies it
	else -- if isnt a number and cant be converted
		warn("Text Must be a Number!")
		return -- Ends function
	end

end

type isn’t as complicated as you think:

type

just simply returns what it is.

print(type(10)) -- number
print(type(true)) -- boolean
print(type("Hello!")) -- string
print(type({"Hi"})) -- table

if type(100) == "number" then -- checks if 100 is a number
print("Yes")
end

so is tonumber and tostring

local StringNumber = "1"
local NewNumber = tonumber(StringNumber)
print(type(NewNumber)) -- Should return as a number
print(NewNumber)

local Number = 7384109
local NewString = tostring(Number)
print(type(NewString)) -- Should return as a string
print(NewString)

Thats not what they are trying to do, They aren’t trying to create a Timer, but simply change the time via TextBox, if you read the Topic, you would know

Just add function(Player, Text) and it should work

Well then you can help him yourself, and yes, I never used “type” before. And yes, I did not understand the post correctly. So, goodbye.