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