So I made a Gui where if you click a button the time will be set to 8 pm. Since this is a localscript put into the Gui, I'm going to need a remote event. So I put it in ReplicatedStorage and put the script in ServerScriptService.
Here's the code for the localscript:
1 | local button = game.StarterGui.ScreenGui.TextButton |
2 | local minutesaftermidnight = ( 20 * 60 ) |
3 | button.MouseButton 1 Down:Connect( function () |
4 | game.ReplicatedStorage.LightingReplicator:FireServer(minutesaftermidnight) |
5 | end ) |
Here's the code for the Server Script:
1 | game.ReplicatedStorage.LightingReplicator.OnServerEvent:Connect( function (player,timegiven) |
2 | game.Lighting:SetMinutesAfterMidnight(timegiven) |
3 | end ) |
I've been adjusting things and it still won't work, and the output won't show any errors. Why does this happen?
Clicking a part to change the time is not working?
This occurs because you're referencing button
via StarterGui
. However a player's current UI is stored in PlayerGui
. The PlayerGui
folder is located inside the player's instance.
You should do something like where you index the PlayerGui
to reference the client's current UI. The :WaitForChild
I am using here is in case the code runs before the GUI exists so the code does not error!
https://developer.roblox.com/ko-kr/api-reference/function/Instance/WaitForChild
01 | local players = game:GetService( "Players" ) |
02 | local client = players.LocalPlayer |
03 |
04 | local playerGui = client:WaitForChild( "PlayerGui" ) |
05 | local screenGui = playerGui:WaitForChild( "ScreenGui" ) |
06 |
07 | local textButton = screenGui:WaitForChild( "TextBox" ) |
08 |
09 | textButton.MouseButton 1 Down:Connect( function () |
10 | --// and so on |
11 | end ) |