Hello everyone who is reading, I hope your day has been going well!
Recently, I’ve been trying to make it where the certain players have the ability to change the time of day. Everytime the player presses the SetButton, it would change to whatever ClockTime value the player put within the Time Textbox. However whenever the player presses the button and fires the event to the server nothing happens. I used a local script to call event from but it doesn’t fire at all but there are no errors in the output.
Any help is very appreciated!
Here’s where the script is:
Local Script:
local event = game.ReplicatedStorage.Events.ChangeTime
local player = game.Players.LocalPlayer
local button = script.Parent
local Time = button.Parent.Time
local function ChangeTime()
event:FireServer(tonumber(Time.Text))
end
button.TouchTap:Connect(function()
event:FireServer(tonumber(Time.Text))
end)
button.MouseButton1Click:Connect(function()
event:FireServer(tonumber(Time.Text))
end)
Server Script:
local lighting = game.Lighting
local Changing = false
local Time = 0.01
local TimeSpeed = 0.0025
while wait(Time) do
lighting.ClockTime = lighting.ClockTime + TimeSpeed
end
game.ReplicatedStorage.Events.ChangeTime.OnServerEvent:Connect(function(player, NewTime)
lighting.ClockTime = NewTime
print("ChangeTime Activated")
end)
just receieved an error 15:33:37.275 Remote event invocation queue exhausted for ReplicatedStorage.Events.ChangeTime; did you forget to implement OnServerEvent? (16 events dropped) - Studio
This wouldn’t Exactly do anything, Everything is already loaded on the Server, WaitForChild would be used more with finding items on the Client as it takes time to download.
Your Issue appears to be the while loop which halts the code until its broken, Since the loop is preventing a new Connection, the game will think there is no Connection for this RemoteEvent, try using task.spawn() or coroutine.create()/coroutine.wrap() to have it run alongside the main code:
task.spawn(function()
while task.wait(Time) do
lighting.ClockTime = lighting.ClockTime + TimeSpeed
end
end)
game.ReplicatedStorage.Events.ChangeTime.OnServerEvent:Connect(function(player, NewTime)
lighting.ClockTime = NewTime
print("ChangeTime Activated")
end)
Also, you can do Variable += Number instead of Variable = Variable + Number
coroutine.wrap(function()
while task.wait(Time) do
lighting.ClockTime += TimeSpeed
end
end)()
This should make it so your loop doesn’t disrupt the code
it worked! Thank you so much for the help everyone, I really appreciate it!
Here’s the new code for anyone who is interested.
local script:
local event = game.ReplicatedStorage.Events:WaitForChild("ChangeTime")
local player = game.Players.LocalPlayer
local button = script.Parent
local Time = button.Parent.Time
local function ChangeTime()
event:FireServer(tonumber(Time.Text))
end
button.TouchTap:Connect(function()
event:FireServer(tonumber(Time.Text))
end)
button.MouseButton1Click:Connect(function()
event:FireServer(tonumber(Time.Text))
end)
server script:
local lighting = game.Lighting
local Changing = false
local Time = 0.01
local TimeSpeed = 0.0025
task.spawn(function()
while task.wait(Time) do
lighting.ClockTime = lighting.ClockTime + TimeSpeed
end
end)
game.ReplicatedStorage.Events:WaitForChild("ChangeTime").OnServerEvent:Connect(function(player, NewTime)
lighting.ClockTime = NewTime
print("ChangeTime Activated")
end)