Unable to assign property Text. string expected, got nil

Local:

local rp = game:GetService("ReplicatedStorage")  -- Service for replicating objects between client and server
local remotes = rp:WaitForChild("Remotes")  -- The Remotes folder in the replicated storage

local player = game:GetService("Players").LocalPlayer  -- Local player
local formatNumber = require(rp:WaitForChild("Libs").FormatNumber.Simple)  -- Module for number formatting

local gui = player.PlayerGui:WaitForChild("MainGui")  -- The player's main GUI interface

-- Function to update currency display
local function updateCurrency(currency: "BunkCoins" | "Coins", amount)
    -- Format the number into a readable format
    --amount = formatNumber.FormatCompact(amount)

    -- If the currency is BunkCoins, update the text in the main interface
    if currency == "BunkCoins" then
        gui:WaitForChild("TextLabel").Text = amount
    end
end

-- Update the display of BunkCoins when the game loads
updateCurrency("BunkCoins", remotes.GetData:InvokeServer("BunkCoins"))

-- Listen for the event of updating the amount from the server and update the display
remotes.UpdateCoinEvent.OnClientEvent:Connect(function(amount)
    updateCurrency("BunkCoins", amount)
end)

Module:

-- Function to adjust the amount of BunkCoins when clicked
function Manager.AdjustBunkCoins(player: Player, amount: number)
    local profile = Manager.Profiles[player]

    -- If the player profile doesn't exist, exit the function
    if not profile then return end

    -- Increase the BunkCoins amount by the specified value
    profile.Data.BunkCoins += amount
    print(profile, profile.Data, profile.Data.BunkCoins, amount)

    -- Notify the client about the change in BunkCoins amount
    remotes.UpdateCoinEvent:FireClient(player, profile.Data.Coins)
end

So I’m making a clicker based on the tutorial:

and this is my script for displaying clicks (coins), the problem is that, sometimes, once in a couple times when entering the game there is this bug, writes an error:

And the value that should be displayed instead of the real value shows what I wrote originally, in short it does not change, what can be the problem?

Would you be fine sending the code related to this RemoteFunction? It appears the RF is not returning any value.

sure

local rp = game:GetService("ReplicatedStorage")  -- Replicated storage service
local remotes = rp:WaitForChild("Remotes")  -- Folder containing remote events

-- Create a table to store player profiles
local Manager = {}

Manager.Profiles = {}

-- Function to adjust the amount of BunkCoins when clicked
function Manager.AdjustBunkCoins(player: Player, amount: number)
    local profile = Manager.Profiles[player]

    -- If player profile doesn't exist, exit the function
    if not profile then return end

    -- Increase the BunkCoins amount by the specified value
    profile.Data.BunkCoins += amount
    print(profile.Data.BunkCoins)

    -- Notify the client about the change in BunkCoins amount
    remotes.UpdateCoinEvent:FireClient(player, profile.Data.Coins)
end

-- Function to get data from player's profile
local function getData(player: Player, directory: string)
    local profile = Manager.Profiles[player]

    -- If player profile doesn't exist, exit the function
    if not profile then return end

    -- Return data from the specified directory in player's profile
    return profile.Data[directory]
end

-- Assign getData function as a callback for remote invocation
remotes.GetData.OnServerInvoke = getData

return Manager

Maybe the player does not have a profile yet, and it returns nil. See if the profile exists or not with a simple print(profile) before the profile check.

u mean here:

local function getData(player: Player, directory: string)
	local profile = Manager.Profiles[player]

	if not profile then return end

	print(profile)
	return profile.Data[directory]
end

?

No, before the if not profile then return end. It should help narrow down the issue.

like this one?:

local function getData(player: Player, directory: string)
	local profile = Manager.Profiles[player]
	
	print(profile)

	if not profile then return end

	return profile.Data[directory]
end
1 Like

nah I have the same problem

1 Like

Yes. We should be able to deduce the issue into a few possibilities:

  • Assuming BunkCoins is a number under profile.Data, if the profile exists, but BunkCoins isn’t, that would be unexpected.
  • If it does not exist, you can use the Players.PlayerAdded event to add a new profile for each player who joins.

hmm, okay, but first of all I’m sure they exist, and the problem is that sometimes do not have time to create or something like that, because I’m testing now and out of 8 attempts (restarts) this bug is not there.

Shouldn’t it be profile.Data.BunkCoins?:

-- Function to adjust the amount of BunkCoins when clicked
function Manager.AdjustBunkCoins(player: Player, amount: number)
    local profile = Manager.Profiles[player]

    -- If the player profile doesn't exist, exit the function
    if not profile then return end

    -- Increase the BunkCoins amount by the specified value
    profile.Data.BunkCoins += amount
    print(profile, profile.Data, profile.Data.BunkCoins, amount)

    -- Notify the client about the change in BunkCoins amount
    remotes.UpdateCoinEvent:FireClient(player, profile.Data.BunkCoins)
end
1 Like

ye, I already changed it, thank you

1 Like

Is the error still occurring? Hopefully it fixed the problem :sweat_smile:

1 Like