Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Script isn't printing anything, only error is on line 3. Code made myself. Why won't it work?

I can't figure out whats wrong with this script.

local plr = game.Players.LocalPlayer.Name
local LocalPlayer = game.Workspace:FindFirstChild(plr)
local Humanoid = LocalPlayer.Humanoid

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)

    if inputObject.KeyCode == Enum.KeyCode.J then
	print('test')
	if Humanoid.HealthDisplayDistance == 100 or Humanoid.NameDisplayDistance == 100 then
		
	    Humanoid.HealthDisplayDistance = 0
	    Humanoid.NameDisplayDistance = 0
	else
	    Humanoid.HealthDisplayDistance = 100
	    Humanoid.NameDisplayDistance = 100
	end
		
    end

end)

1 answer

✓ Accepted answer

The humanoid is not a child of the player. Neither is the character. The character of a player is a property that points to an instance in the workspace. Inside of that instance is the humanoid.

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

game:GetService("UserInputService").InputBegan:connect(function(inputObject, gameProcessedEvent)

    if inputObject.KeyCode == Enum.KeyCode.J then
    print('test')
    if Humanoid.HealthDisplayDistance == 100 or Humanoid.NameDisplayDistance == 100 then

        Humanoid.HealthDisplayDistance = 0
        Humanoid.NameDisplayDistance = 0
    else
        Humanoid.HealthDisplayDistance = 100
        Humanoid.NameDisplayDistance = 100
    end

    end

end)

This discussion is preserved as it was. New answers, comments, and votes are closed.