Need help fixing this nil index error

This line on my client-side script keeps erroring how do I fix it?

local WarningGUI = PlayerToWarn.Character.Head.WarningGUI

The Full Client-Side Script:

local Event = game.ReplicatedStorage:WaitForChild("Warning") -- add an event called "Warning" in ReplicatedStorage
local filterFunction = game.ReplicatedStorage:WaitForChild("FilterWarn") -- add a function called "FilterWarn" in ReplicatedStorage

Event.OnClientEvent:Connect(function(Reason,PlayerToWarn)
	local WarningGUI = PlayerToWarn.Character.Head.WarningGUI
	local CurrentWarnings = WarningGUI.Warnings
	local filteredText = filterFunction:InvokeServer(Reason) -- will filter the warn reason

	CurrentWarnings.Value = CurrentWarnings.Value + 1
	WarningGUI.WarningLabel.Text = "W" .. CurrentWarnings.Value .. " - " .. Reason

	if CurrentWarnings.Value >= 3 then
		PlayerToWarn:Kick("You've reached the maximum number of warnings and have been kicked from the server.")
	end
end)

The Error:

Workspace.NotAid_n.Head.WarningGUI.ClientSideWarning:5: attempt to index nil with ‘Character’

18:35:41.762 - Stack Begin

18:35:41.764 - Script ‘Workspace.NotAid_n.Head.WarningGUI.ClientSideWarning’, Line 5

18:35:41.764 - Stack End

Seems self explanatory, no? Read the error carefully. It’s telling you that you’re trying to index something that doesn’t exist. In this case, PlayerToWarn is coming out as nil and you can’t index anything on nil.

I have a feeling that the cause of this involves what you’re doing from FireServer because the code otherwise looks fine. Can you share exactly what you do from the server?

1 Like

this is the server script:

local GroupId = 6741421
local MinimumRankToUseCommand = 9

game.Players.PlayerAdded:Connect(function(Player)
	Player.CharacterAdded:Connect(function(Character)
		local WarningGUI = script.WarningGUI:Clone()
		WarningGUI.Parent = Character.Head
	end)
	
	local TextService = game:GetService("TextService") -- TextService variable
	
	Player.Chatted:Connect(function(Message)
		local SplitMessage = Message:split(" ")
		if SplitMessage[1] == "!warn" and Player:GetRankInGroup(GroupId) >= MinimumRankToUseCommand then
			local NameOfPlayerToWarn = SplitMessage[2]
			local PlayerToWarn = game.Players:FindFirstChild(NameOfPlayerToWarn)
			local Reason = Message:split(NameOfPlayerToWarn)[2]

			local WarningGUI = PlayerToWarn.Character.Head.WarningGUI
			local CurrentWarnings = WarningGUI.Warnings

			local playerName = PlayerToWarn.Name

			game.ReplicatedStorage.FilterWarn.OnServerInvoke = (function(playerName, Reason) -- server receives invoke
				local filteredTextResult = TextService:FilterStringAsync(Reason, Player.UserId) -- filters string
				return filteredTextResult:GetNonChatStringForBroadcastAsync() -- returns filtered string to the client
			end)

			game.ReplicatedStorage.Warning:FireClient(PlayerToWarn, Reason)
		end
	end)
end)

Has to do with your server and the FireClient call. You may want to read that article to understand a bit about how FireClient works, which would also reveal why your script is failing.

The way the parameters on FireClient work is that the first one determines which client should receive the event and the rest of them are sent as arguments. When a client receives an event with OnClientEvent, they see all the arguments except the first one. So in reality the client is only receiving one argument here, the reason. The second one, the player, is nil.

Adding the PlayerToWarn variable again as the third argument of FireClient will fix the system at the very least. It’s worth asking why you’d kick the client from the client for a moderation system though… seems very insecure.

game.ReplicatedStorage.Warning:FireClient(PlayerToWarn, Reason, PlayerToWarn)
1 Like

I’ll try that and read the article, I’m not super worried as I have a ban system for exploiters that is VERY secure.

@lesserfantasy It works but the filter doesn’t now. How do I fix that?

You’re going to need to provide a whole lot more detail than “now my code doesn’t work”.

1 Like

It says in in-game devconsol:

Infinite yield possible on 'replicatedstorage:WaitForChild(“Warning”)

also

something about line one of the client-side script.

That means that “Warning” is not showing up in replicated storage.

1 Like

Oh okay I replaced it with the actual listing of its location instead I am testing that in-game.

Also, rap the WaitForChild in a pcall in case something goes wrong.

It’s there but for some reason it says it isn’t :confused:

1 Like

Is “Warning” the exact name and is it deleted in run time?

1 Like

Infinite yield means that the object can wait forever. Example: if a player has issues connecting an inf yield will happen.

1 Like

weird, yes the exact name and it isn’t deleted during runtime.

Are you creating this event on the server? I tried creating events and it did not work, it’s not worth it. If you’re cloning something into replicated storage be careful. Also make sure you’re ACTUALLY referencing replicated storage.