I can't detect "Head" in server script

I did the following:

A ServerScript will detect if RPName.TextLabel(called Name) matches player.Name.
If it matches it will add a Tag to the Chat.
If not, nothing will happen.

Code:
local ChatService = require(game:GetService(“ServerScriptService”):WaitForChild(“ChatServiceRunner”):WaitForChild(“ChatService”))

ChatService.SpeakerAdded:Connect(function(player)
local Speaker = ChatService:GetSpeaker(player)
local character = player.Character
local function xa()
while wait(1) do
if player.Name == character.Head.RPName:FindFirstChild(“Name”).Text then
print(1)
Speaker:SetExtraData(–[[someting]])
Speaker:SetExtraData(–[[someting]])
Speaker:SetExtraData(–[[someting]])
else

end
end
end
xa()
end)

Place:test.rbxl (22.3 KB)

What’s the problem?
For some reason I get the following error:image

What am I doing wrong?
Note: for those who downloaded the file, the RPName does not exist in this place, as the problem is not with its location but with the location of the Head.

Its because this

local character = player.Character

is equals to nil becaue the players character has not loaded in yet meaning it does not exist.

to stop this just add this line above it:

repeat wait() until player.Character
1 Like

print(“aaaa”)
repeat wait() until player.Character
print(“bbbb”)
local character = player.Character

Looks like that way he’ll never imbibe “bbbb”

can you explain more what you mean I dont understand?

1 Like


putting the new line above, now nothing will be read below line 6.
I was supposed to print “finish” but nothing happens.
It was also supposed to give an error that the RPName does not exist but nothing happens in the output either

Ok I didn’t know that the connection return a string so just change the repeat wait() line to

repeat wait() until game.Players:FindFirstChild(player).Character
local character = game.Players:FindFirstChild(player).Character
1 Like

SpeakerAdded returns the speakername (playername), not the player instance.
You can use Speaker:GetPlayer(), example:

local ChatService = require(ChatServiceModuleLocation)


local function SpeakerAdded
(SpeakerName)
	local Speaker = ChatService:GetSpeaker(SpeakerName)
	local Player = Speaker:GetPlayer()
	if (Player)
	then
		local Character = Player.Character or Player.CharacterAdded:Wait()
		local Head = Character.Head
		-- Code.
	end	
end


local GetSpeakers = ChatService:GetSpeakerList()
for i = 1, (#GetSpeakers)
do
	task.spawn(SpeakerAdded, GetSpeakers[i])
end
ChatService.SpeakerAdded:Connect(SpeakerAdded)
1 Like

There’s no need for a loop like that, read my post for a fixed solution.

1 Like