Unable to assign property Text. string expected, got nil
This issue occurs when we try to set the text of a text label to a nil value. This issue does not have one specific cause because it entirely depends on the value you were using to set the text of the text label to. It’s extremely circumstantial and requires you to do some debugging of your own. We can at least point you in the right direction by saying that you should investigate why the value attempting to use is nil.
I’ll showcase a short code example to hopefully help make clearer what you need to look into:
local Players = game:GetService("Players")
local player = Players.LocalPlayer
local playerGui = player.PlayerGui
local gui = playerGui:WaitForChild("Example") -- Screen Gui
local textLabel = gui.TextLabel -- Text Label
local amount = 0
textLabel.Text = amount -- The text label will display 0
amount = nil
textLabel.Text = amount -- An error will be thrown because amount is nilClicking Simulator Issue
We encounter this issue during our 3rd episode of the Clicking Simulator series when we attempt to update the player’s currency text labels. This occurred because we attempted to get the player’s data from the server before it was loaded by using a remote function. Since the data was not loaded, instead of getting the player’s data we received nil. So when we attempted to update the text label with what we thought was the player’s data, we instead were setting the text to nil.
If you’re currently going through this series and experiencing this exact issue then there are a few temporary solutions that I will go over. This is eventually solved entirely once we create our State manager.
Disabling API Services
The reason that I did not encounter this issue during my video is because I had my API services disabled. It wasn’t something I considered at the time, but this would effectively skip the short amount of time it took for the player’s data to load. So in a way, my data was already loaded on the server as soon as I started the game. That’s why I received a number rather than nil when using the remote function.
You can disable the API Services by following these steps:
- Go to the Home tab in the topbar of Studio
- Select the Game Settings button
- Select the Security tab
- Toggle the Enable Studio Access to API Services setting off
Using a Default Value
Since we receive an error when setting the text of a text label to nil another option we have is to set the text to something else if the value is nil. This is another temporary solution and certainly not a great one, but I believe that it has some value in the regard that it can be learned from.
So when we’re setting the text of the player’s currency text label, we’ll set the text based on if the provided value is nil or not. If the value is nil then we’ll set the text to 0, otherwise it’ll be set to the value.
...
local function UpdateCurrency(currency: "Clicks" | "Gems", amount: number)
amount = if amount then amount else 0 -- If amount is nil, then it will become 0
if currency == "Clicks" then
Clicks.Text = amount
elseif currency == "Gems" then
Gems.Text = amount
end
end