Scripts don't understand what the character is

When using: "game.Players.LocalPlayer.Character " in a LocalScript, and defining any child inside of the character, it will always return an error along the lines of “Attempt to index nil with”. This is inside a function that is called when the player chats, so I think it doesn’t have to do with me calling for the character too early. If you also do game.Workspace:FindFirstChild(game.Players.LocalPlayer.Name) in a LocalScript, and defining and child inside the character, it will also return the same error. Here is my code:

local prefix = ':'

function uwu(msg)
	if msg:split(' ')[1]:lower() == prefix..'kill' and msg:split(" ")[2] then
		for i=1,#msg:split(' ') do
			for i, v in pairs(game.Players:GetChildren()) do
				if v.Name:sub(1,i):lower() == msg:split(" ")[2]:sub(1,i):lower() then
					game.Workspace:FindFirstChild(v).Humanoid.Health = 0
				end
			end
		end
	end
end

game.Players.PlayerAdded:Connect(function(plr)
	plr.Chatted:connect(function(msg)
		uwu(msg)
	end)
end)

Any help is appreciated. :smiley:

1 Like

There’s the possibility that your code is trying to reach for the player character when the player didn’t spawn yet. Instead of using game.Players.LocalPlayer.Character try using:

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()

This variable will be the player’s character object, and if the player does not have a character yet, it will yield until the player character spawns.

On the script you provided, try adding a if statement to check if the player does has a character, if not then you can stop the code or wait for the character. A player that didn’t spawn yet won’t have a character model in the workspace. And on your script you should change game.Workspace:FindFirstChild(v).Humanoid.Health = 0 to game.Workspace:FindFirstChild(v.Name).Humanoid.Health = 0, as in this case “v” is a player object, not a string.

game.Workspace:FindFirstChild(v.Name).Humanoid.Health = 0
This line is not necessary ,
you can just do

local char = v.Character
if char then -- To Check if Character is nil(here not loaded)
 char:FindFirstChild("Humanoid").Health = 0 -- You can use :TakeDamage for taking damage
end

Furthermore, using FindFirstChild like this is redundant. You’re supposed to use FindFirstChild when you aren’t sure if something is going to exist or not in order to prevent an error. If the Humanoid doesn’t exist here, you’ll get an error about trying to index “Health” of nil. A possible solution can be this:

if char and char:FindFirstChild("Humanoid") then
    char.Humanoid.Health = 0
end

The chances of it becoming nil is very less, but still a good one not something to be bothered about . If was writing the code I will just make a variable for humanoid like : local humanoid = char:WaitForChild("Humanoid") inside the if condition then take the damage.

Hi, thank you for the response. Before this I did try game.Players.LocalPlayer.Character, but forgot to change it back. Regardless, it still outputs the error.

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
repeat wait() until character:FindFirstChild("Humanoid")
1 Like

That code is actually incorrect, Character is a property of a Player, not a child.
Therefore that would infinitely yield if the character does not load before the script finishes compiling.
This would be correct:

local Character = game:GetService("Players").LocalPlayer.Character or game:GetService("Players").LocalPlayer.CharacterAdded:Wait()

Another incorrect thing, you shouldn’t be yielding using wait() loops and should instead be relying on events or WaitForChild.

local character = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")