Notes ~ this is inside StarterPlayerScripts, which has an Integer Value named "Air", this is a local script inside of "Air"
Hey all, so I'm trying to make a game where you have a set amount of oxygen and when you run out it starts to damage you. So I made the first part work
local player = game.Players.LocalPlayer -- getting the player local air = script.Parent -- the IntValue while wait(1) do -- every one second if air.Value > 0 then -- check if any air is remaining air.Value = air.Value - 1 -- subtract an air print(air.Value.." air remaining.") -- print the air remaining end
However, when I made some modifications to the script (see below), it stopped working and gave me this error
Players.v1rtical.PlayerScripts.Air.LocalScript:3: attempt to index nil with 'FindFirstChild'
Here is the new (broken) script
local player = game.Players.LocalPlayer local air = script.Parent local humanoid = player.Character:FindFirstChild("Humanoid") while wait(1) do if air.Value > 0 then air.Value = air.Value - 1 print(air.Value.." air remaining.") else print("No air remaining!") humanoid.Health = humanoid.Health - 10 end end
Hope someone can help, thanks!!
This is because the Player's Character can sometimes take too long to load. Simply add a CharacterAdded:Wait()
to yield the Character Object return until it is available.
local Player = game:GetService("Players").LocalPlayer local Character = Player.Character or Player.CharacterAdded:Wait() local Humanoid = Character:FindFirstChildOfClass("Humanoid")
You are looking at the wrong part of the player. There are two instances that correspond to one player: a player instance that is located in game.Players that contains player scripts, etc, and a model of the player that appears in game.Workspace once you join the game, that contains physical aspects of the player such as the avatar parts and the Humanoid.
In order to find the Humanoid, you have to look for the model of the player in game.Workspace
local playerCharacter = game.Workspace:FindFirstChild(player.Name)
and then look for the Humanoid within the player model
local humanoid = playerCharacter:FindFirstChild("Humanoid")