I made a GUI where you enter the time you want it to be in MinutesAfterMidnight. but whenever i hit the enter button, it says "Unable to cast string to double" and I can't seem to find out what it means simply by googling it. Can someone explain what this means and maybe a fix?
Your problem is that game.StarterGui.ScreenGui.TextBox.Text
is a string type, meaning it is text. SetMinutesAfterMidnight
. An easy fix is to type tonumber(game.StarterGui.ScreenGui.TextBox.Text)
.
However, that is not your only problem
Another problem I encountered is this: game.StarterGui.ScreenGui.TextBox
When players edit the textbox in their Gui, it is edited in game.Players.LocalPlayer.PlayerGui
, and not game.StarterGui
, therefore, you will have to set your code to this:
script.Parent.MouseButton1Click:Connect(function() print("clicked") game.Lighting:SetMinutesAfterMidnight(tonumber(game.Players.LocalPlayer.PlayerGui.ScreenGui.TextBox.Text)) print("entered") end)
You may be like "Wait a minute, this is super complex code"!
You can actually tidy this up with variables
local SetTimeButton = script.Parent local Players = game:GetService("Players") local Player = Players.LocalPlayer local MinutesAfterMidnightInput = Player.PlayerGui.ScreenGui.TextBox local Lighting = game:GetService("Lighting") SetTimeButton.Activated:Connect(function() print("Clicked!") Lighting:SetMinutesAfterMidnight(tonumber(MinutesAfterMidnightInput.Text)) print("Set time successfully!") end)
Edit 1: Fixed error I just noticed