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.
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.
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.
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
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.
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.