StringValue.Changed doesn't fire function

Exactly what the title says. StringValue.Changed isn’t firing the function.

I’ve enclosed part of the function in question, here:

function HandleCharacter(player)
	local cName = WaitForChild(player, 'CharacterName')	
	local character = game.Lighting.Characters[cName.Value]:Clone() 
	
	character.Parent = game.Workspace
	character.Name = player.Name
	player.Character = character
end

as well as the StringValue.Changed part of the code that doesn’t fire

function HandlePlayer(player)
	local cName = Instance.new('StringValue')
	cName.Name = 'CharacterName'
	cName.Value = ''
	cName.Parent = player 


	cName.Changed:connect(function() 
		if cName.Value ~= '' then 
			HandleCharacter(player)
		end 
	end)	
end

game.Players.ChildAdded:connect(function(player)
	HandlePlayer(player)
end)

for _, player in pairs(game.Players:GetPlayers()) do 
	if player:IsA('Player') then 
		HandlePlayer(player) 
	end
end

I’ve tried rewriting the code that calls the function in many different ways, such as

cName.Changed:Connect(HandleCharacter(player))
end

but that just gives me an error ( is not a valid member of Model “Lighting.Characters”) because it’s calling the function before I’ve selected my character.

I’ve also looked all over the devforum but haven’t come up with any solutions.

Any help would be appreciated. Been stuck on this for days.

do a primt to see if its not running or sum else

I’ve done a print, that’s actually one of the first things I did. Nothing gets printed out which is why I came to the conclusion that the cName.Changed isn’t connecting the function properly.

well connect is deprecated capatlize the C but thats not the issue

local cName = WaitForChild(player, ‘CharacterName’) is wrong its

local cName = Player:WaitForChild(“CharacterName”)

Did you make a function named WaitForChild()? If so, show us the code, I doubt the function isn’t connected to the event. Try doing a print statement that prints something whenever the event gets fired.

cName:GetPropertyChangedSignal("Value"):Connect(function()

Nope, that didn’t work either. It’s gotta be something to do with cName.Changed because when I put a print at the top of function HandleCharacter(), nothing gets printed in the output even though cName.Value is being changed.

local function WaitForChild(instance, name)
	while not instance:FindFirstChild(name) do
		instance.ChildAdded:wait()
	end
	return instance:FindFirstChild(name)
end

can i see where the val chanhes

I think you’ve been misinformed about this problem.

The problem is not about the event not firing, it is indeed connected and listening, the real problem is that it cannot find the specified instance with the given name, in this case, you left cName.Value = ' ', then the script will attempt to find an instance with that name, which it doesn’t exist, so it errors it.

The value changes whenever I click on a character in CharSelect GUI, but the function isn’t being called properly.

Hopefully this video uploads, if not I’ll post again.

I’m ALMOST POSTIVIE it’s just not firing. Even if I take everything out of HandleCharacter() and have it literally just be

function HandleCharacter(player)
print("Working")
end

it still doesn’t work. Nothing gets printed.

Oh bruh now I see the problem.

During the creation of the StringValue, you set the value to this.

Then, when it meets this if statement, this statement would return false because both strings matches. It only returns true if either you set it to equals to instead of not equals to OR change the string to something else.

The reason I set it up like that is because when a character is selected, the cName.Value is changed from the initial blank value. If

 cName.Value ~=  ''

then it should fire the function once the character is selected, no?

This will refuse to return true until cName.Value is something else, so yeah.

So that can’t be the problem either then, because even when the value is something other than ’ ', it still doesn’t connect the function as it should.

That’s because it’s returning false. Have you tried setting cName.Value to something else?

Yeah I initially tried setting it as

cName.Value = 'undecided'

and then once it changed have it connect the function, but it still must be returning false.

Then could you try changing the lowercase c in connect to uppercase?