Attempt to index nil with 'Humanoid' error

Hello, I’m trying to make a glass of water tool for the player to drink.
However, I can’t get the animation to play due to this error:
You help is much appreciated!

This is my script:

--Variables

local player = game:GetService("Players").LocalPlayer

local Animation = script.Parent:FindFirstChild("Drink")

local Character = player.Character

local Slurp = script.Parent.Handle:FindFirstChild("Sound")

local DrinkAnim = Character.Humanoid:LoadAnimation(Animation)

local canDrink = true

local Cooldown = 2.4

--Drink

script.Parent.Activated:Connect(function()

if canDrink then

canDrink = false
DrinkAnim:Play()
Slurp:Play()
wait(Cooldown)
canDrink = true

end

end)

You need to wait untill character is loaded.

Simply you can use this

repeat wait() until Character

pasted after definition of Character.

1 Like

For anyone else who will read this topic, I suggest to instead use

local Character = player.Character or player.Character:Wait()

This should give a more accurate time than

repeat wait() until player.Character

As well as it will not check every >0.1 seconds for the player.Character and instead wait for a response and if the player is already loaded it will not run the first wait()

1 Like