Variable is turning nil after using again

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
    When obtaining the variable the second time for it not to end up as nil

  2. What is the issue? Include screenshots / videos if possible!
    Console:
    image
    Script:

  3. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
    I’ve looked all over the DevForum and have found absolutely nothing, I’ve ran print statements to ensure that nothing stops the code and nothing appears to be stopping it besides the value turning nil randomly

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

-- Server Script
ReplicatedStorage.Remotes.NamePicked.OnServerEvent:Connect(function(Player : Player, Action, ChosenName, GUI)
	local TextService = game:GetService("TextService")
	local Slot = Slots[Player.UserId]["Slot"]
	
	repeat
		task.wait()
	until GUI
	
	local Overlay = GUI:FindFirstChild("Overlay")
	local Confirm = Overlay:WaitForChild("Confirm")
	local Warn = Overlay:WaitForChild("Warn")

	local CharacterData = DataStoreModule.find("PlayerData", "Player_" .. Player.UserId, Slot)

	local FilteredName
	
	if Action == "EnteredName" then
		local Success, Result = pcall(TextService.FilterStringAsync, TextService, ChosenName, Player.UserId)
		
		if Success then
			FilteredName = Result:GetNonChatStringForBroadcastAsync()
			print(FilteredName)
			
			if FilteredName:match("[^a-zA-Z]+") then
				Confirm.Visible = false
				Warn.Visible = true
				task.delay(2, function()
					Warn.Visible = false
				end)
			else
				print(FilteredName)
				Confirm.Visible = true
			end
		else
			Confirm.Visible = false
			Warn.Visible = true
			task.delay(2, function()
				Warn.Visible = false
			end)
		end
	elseif Action == "FinalizeName" then
		
		GUI:Destroy()
		
		print(FilteredName)
		CharacterData.Value.FirstName = FilteredName
		print(CharacterData.Value.FirstName)
	end	
end)
-- Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local Overlay = script.Parent:WaitForChild("Overlay")
local TextBox = Overlay.TextBox
local Warn = Overlay.Warn
local Confirm = Overlay.Confirm

TextBox.FocusLost:Connect(function(EnterPressed)
	if TextBox.Text ~= "" and (string.len(TextBox.Text) > 3) and (string.len(TextBox.Text) < 12) and EnterPressed then
		print(EnterPressed)
		Remotes.NamePicked:FireServer("EnteredName", TextBox.Text, script.Parent)
	end
end)

Confirm.MouseButton1Click:Connect(function()
	if Confirm.Visible == false then
		return
	else
		Remotes.NamePicked:FireServer("FinalizeName", TextBox.Text, script.Parent)
	end
end)

Please do not ask people to write entire scripts or design entire systems for you. If you can’t answer the three questions above, you should probably pick a different category.

1 Like

Right after I check if the Action is “FinalizeName”, The variable turns nil and I have no clue what the reason could be

1 Like

Each call to the remote event creates a new thread and as filteredname is declared inside the connected function it is a new variable each time.

To fix you just need to move local filteredname to before the remote event connection. Now filteredname will share the same scope with every call to the remoteevent.

Still not ideal as it will change every time any player calls the remote!
So if we convert it to a table

local filterednames ={}

And index it with the player inside the remote event function

filterednames[player] = filteredname

We can keep track of and change everyone’s filteredname

1 Like

The reason why FilteredName is becoming nil is because you’re doing FilteredName:match(“[^a-zA-Z]+”). If the result is false the name value is being changed to nil so instead of:

if FilteredName:match("[^a-zA-Z]+") then

You need to do something like this:

local textMatchResult = FilteredName:match("[^a-zA-Z]+")

if textMatchResult then

It still returns to nil when detecting “FinalizeName”

The issue is where FilteredName is nil when you try to print it in the “FinalizeName” action. This is because FilteredName is only defined inside the “EnteredName” action branch.
you need to define FilteredName outside the scope of the “EnteredName” branch so that it is accessible later in the “FinalizeName” branch.

Server script:

ReplicatedStorage.Remotes.NamePicked.OnServerEvent:Connect(function(Player, Action, ChosenName, GUI)
    local TextService = game:GetService("TextService")
    local Slot = Slots[Player.UserId]["Slot"]

    repeat
        task.wait()
    until GUI

    local Overlay = GUI:FindFirstChild("Overlay")
    local Confirm = Overlay:WaitForChild("Confirm")
    local Warn = Overlay:WaitForChild("Warn")

    local CharacterData = DataStoreModule.find("PlayerData", "Player_" .. Player.UserId, Slot)

    local FilteredName

    if Action == "EnteredName" then
        local Success, Result = pcall(TextService.FilterStringAsync, TextService, ChosenName, Player.UserId)

        if Success then
            FilteredName = Result:GetNonChatStringForBroadcastAsync()
            print(FilteredName)

            if FilteredName:match("[^a-zA-Z]+") then
                Confirm.Visible = false
                Warn.Visible = true
                task.delay(2, function()
                    Warn.Visible = false
                end)
            else
                print(FilteredName)
                Confirm.Visible = true
            end
        else
            Confirm.Visible = false
            Warn.Visible = true
            task.delay(2, function()
                Warn.Visible = false
            end)
        end
    elseif Action == "FinalizeName" then
        GUI:Destroy()

        if FilteredName then
            print(FilteredName)
            CharacterData.Value.FirstName = FilteredName
            print(CharacterData.Value.FirstName)
        else
            warn("FilteredName is nil. Please choose another name.")
        end
    end
end)

Local Script:

-- Local Script
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Remotes = ReplicatedStorage:WaitForChild("Remotes")

local Overlay = script.Parent:WaitForChild("Overlay")
local TextBox = Overlay.TextBox
local Warn = Overlay.Warn
local Confirm = Overlay.Confirm

TextBox.FocusLost:Connect(function(EnterPressed)
    if TextBox.Text ~= "" and (string.len(TextBox.Text) > 3) and (string.len(TextBox.Text) < 12) and EnterPressed then
        print(EnterPressed)
        Remotes.NamePicked:FireServer("EnteredName", TextBox.Text, script.Parent)
    end
end)

Confirm.MouseButton1Click:Connect(function()
    if Confirm.Visible == false then
        return
    else
        Remotes.NamePicked:FireServer("FinalizeName", TextBox.Text, script.Parent)
    end
end)

I want the player to have their own custom first name, why would I want to set it to “DefaultName”?

1 Like

Even with this, the name always comes as nil no matter what you do to change it. It prints but then when gotten it returns as nil

Wait is this changing a Textlabel like a overhead or the players name???

Try using string.match(FilteredName, "[^a-zA-Z]+") instead of FilteredName:match("[^a-zA-Z]+")

It’s changing the player’s data, the first name will be used on their humanoid display name

Not possible, You cannot change the Players name as it won’t change in “Players” service even if you get the Characters model Name to change still won’t work.

I’m not actually changing their name but rather their in-game data, Like first name, last name, race, gold coins

I just saw this, for some reason it didn’t appear until I refreshed and scrolled up. It did end out working, I didn’t realize that the table refreshes everytime the event is called

1 Like

I see, how is the Name data changing again?

CharacterData is the variable for the datastore

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