Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

"Unable to cast string to double"?

Asked by 4 years ago

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?

0
Post your code? User#30567 0 — 4y
0
@TTChaos script.Parent.MouseButton1Click:Connect(function() print("clicked") game.Lighting:SetMinutesAfterMidnight(game.StarterGui.ScreenGui.TextBox.Text) print("entered") end) lemony_dev 9 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago
Edited 4 years ago

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

0
thanks! lemony_dev 9 — 4y
0
it works! lemony_dev 9 — 4y
0
No problem! User#30567 0 — 4y
Ad

Answer this question