LocalPlayer.Character getting nil

Hey there,
I want to make a safe zone for players those arent participating in rounds.
It depends on the Character´s parent.
Anyways… im getting this error:
attempt to index nil with ‘Character’ — Line 1

I hope anyone has got a solution for this issue.

game.Players.LocalPlayer.Character.Parent.Changed:Connect(function(hit)

	local char = hit.Parent
	local humanoid = char:FindFirstChild("Humanoid")

	if humanoid ~= nil and humanoid.Parent == workspace.InGame then
		humanoid.MaxHealth = 100
		humanoid.Health = 100
	elseif humanoid ~= nil and not humanoid.Parent == workspace.InGame then
		humanoid.MaxHealth = math.huge
		humanoid.Health = math.huge
	end

end)



1 Like

Is this in a localscript or a normal script? You can’t use localplayer in a normal script. If it’s a normal script, you’ll have to make a loop like this;

for _, plr in next, game.Players:GetPlayers() do --Goes thru all the players in the game
	local char = plr.Character;
	local humanoid = char:FindFirstChild("Humanoid");

	if humanoid ~= nil and humanoid.Parent == workspace.InGame then
		humanoid.MaxHealth = 100
		humanoid.Health = 100
	elseif humanoid ~= nil and not humanoid.Parent == workspace.InGame then
		humanoid.MaxHealth = math.huge
		humanoid.Health = math.huge
	end
end)
1 Like

If this is a server script, you cannot use LocalPlayer as the Server environment does not create the LocalPlayer property.

Oh, then i gotta find another solution.
Thanks anyway :smiley: