"Value of type nil cannot be converted to a number"

Hello Devfourm, I am trying to get a Textbox’s text from the client and send it to the server through a RemoteEvent. When I get the text from the textbox and send it through the RemoteEvent that was to set a part in the workspace transparency to the textbox’s text, doing that I am using the tonnumber() function. When the Textbox’s text is sent to the function it outputs

“value of type nil cannot be converted to a number”

Here is the server script receiving the event:

local Event = script.Parent.Change
local Part = game.Workspace:WaitForChild("Color_Part")

Event.OnServerEvent:Connect(function()
	Part.Transparency = tonumber(Event)
end)

Thank You :slight_smile:

1 Like

It think it doesn’t know what “Event” is.

I would ususally send the variable to the event.

Event.OnServerEvent:Connect(function(value)
Part.Transparency = tonumber(value)
end)

You would need to send the value.

Event:Fire(value)

Define “value” as the text you want

As in value = textbox.Text

When I do that, it gets the local player.

image

The new script:

Local Script:

local Button = script.Parent
local TextBox = script.Parent.Parent.TextBox
local Event = script.Parent.Change
local value = TextBox.Text

Button.MouseButton1Click:Connect(function()
	Event:FireServer(value)
end)

Server Script:

local Event = script.Parent.Change
local Part = game.Workspace:WaitForChild("Color_Part")

Event.OnServerEvent:Connect(function(value)
	print(value)
	Part.Transparency = tonumber(value)
end)

The first argument of OnServerEvent is always a Player. To reference the value being sent, use the second argument instead:

Event.OnServerEvent:Connect(function(player, transparency: string)
	print(transparency)
	Part.Transparency = tonumber(transparency)
end)

it stopped showing the error, but it just prints a blank space now.

First value is always the player, so you have to first specify player and then the value

You have:

local value = TextBox.Text

Button.MouseButton1Click:Connect(function()
	Event:FireServer(value)
end)

So the value is defined before it exists.

Change to be:

Button.MouseButton1Click:Connect(function()
local value = TextBox.Text
Event:FireServer(value)
end)

The problem here is the „Event“ in the code. „Event“ is an already set variable which contains an instance located at script.Parent.Change. This instance itself is not compatible with tonumber().
As others already have mentioned, you need to pass the text in the LocalScript you fire the event. First variable is the player instance that the LocalScript fired from and second variable is the one you passed.

Now, you said when printing out that second argument, it returns blank. Could you print out the value before you pass it like this:

local Button = script.Parent
local TextBox = script.Parent.Parent.TextBox
local Event = script.Parent.Change

Button.MouseButton1Click:Connect(function()
    print(TextBox.Text)
	Event:FireServer(TextBox.Text)
end)

If this returns blank then your textbox seems to contain no text.

That worked! Thanks for the reply!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.