Why CharacterAdded doesn't work in my script?

_G.CurrentConnection=nil
local function Split(Text,Value)
  assert(Value)
  if Text:match(Value) then
  local result=Text:gsub('\32',''):sub(Text:gsub('\32',''):find(Value)+rawlen(Value),rawlen(Text:gsub('\32','')))
   return result
  else
    print[[Given Value Was Not In The Text]]
    return nil
  end
end
function OnChatted(Msg)
  if Msg:lower():match'g_spy' then
  Msg=Split(Msg:lower(),'g_spy')
    for i,v in next,game:GetService'Players':GetPlayers() do
        if v.Name:lower():sub(1,#Msg)==Msg then
            workspace.Camera.CameraSubject=v.Character:FindFirstChild'Humanoid';
          if _G.CurrentConnection==nil then
            _G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
             workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
            end)
          elseif _G.CurrentConnection then
            _G.CurrentConnection:Disconnect()
            _G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
             workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
            end)
          end
            return
        elseif v.DisplayName:lower():sub(1,#Msg)==Msg then
            workspace.Camera.CameraSubject=v.Character:FindFirstChild'Humanoid';
          if _G.CurrentConnection==nil then
            _G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
             workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
            end)
          elseif _G.CurrentConnection then
            _G.CurrentConnection:Disconnect()
            CurrentConnection=v.CharacterAdded:Connect(function(chr)
             workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
            end)
          end
          return
       end
    end
  end
end
game:GetService'Players'.LocalPlayer.Chatted:Connect(OnChatted)

Does it throw any errors/warnings into the console?

For now, and if the everything else seems to be working, perhaps attempt to interchange the :FindFirstChild("Humanoid") for :WaitForChild("Humanoid") since at the moment the Player.CharacterAdded Event is fired the Humanoid object may or may not have loaded in yet.

Everything works fine , no errors i even tried print(type(_G.CurrentConnection)) and it outputs connection as expected, , but when the chosen players’s character gets added it characteradded doesn’t work and the camera doesn’t get updated

I see. Thanks for the clarification.

I think I have found the error: In line 37, you call CurrentConnection, when it should be _G.CurrentConnection .
This code should work:

_G.CurrentConnection=nil
local function Split(Text,Value)
	assert(Value)
	if Text:match(Value) then
		local result=Text:gsub('\32',''):sub(Text:gsub('\32',''):find(Value)+rawlen(Value),rawlen(Text:gsub('\32','')))
		return result
	else
		print[[Given Value Was Not In The Text]]
		return nil
	end
end
function OnChatted(Msg)
	if Msg:lower():match'g_spy' then
		Msg=Split(Msg:lower(),'g_spy')
		for i,v in next,game:GetService'Players':GetPlayers() do
			if v.Name:lower():sub(1,#Msg)==Msg then
				workspace.Camera.CameraSubject=v.Character:FindFirstChild'Humanoid';
				if _G.CurrentConnection==nil then
					_G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
						workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
					end)
				elseif _G.CurrentConnection then
					_G.CurrentConnection:Disconnect()
					_G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
						workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
					end)
				end
				return
			elseif v.DisplayName:lower():sub(1,#Msg)==Msg then
				workspace.Camera.CameraSubject=v.Character:FindFirstChild'Humanoid';
				if _G.CurrentConnection==nil then
					_G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
						workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
					end)
				elseif _G.CurrentConnection then
					_G.CurrentConnection:Disconnect()
					_G.CurrentConnection=v.CharacterAdded:Connect(function(chr)
						workspace.Camera.CameraSubject=chr:FindFirstChild'Humanoid';
					end)
				end
				return
			end
		end
	end
end
game:GetService'Players'.LocalPlayer.Chatted:Connect(OnChatted)

Let me know how it went.

Oh man how absent minded am i everywhere i literally do these kind of mistakes every where like forgetting to do a part of a thing . Let me use it , I’ll answer you as soon as possible.

I tried it , but again, when the chosen player did reset it didn’t update the camera to the chose player’s Humanoid

Maybe it’s because the event detects when the character is added, if the character is already added it the code won’t run. May be instead of connecting a function on the event, use CharacterAdded:Wait().

It is because that i am using it in a local script and it lags a bit because of my wifi so i tried WaitForChild and worked

1 Like

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