Attempt To Index nil with WaitForChild and Animation Not Playing

im trying to make a basketball and when i press F it did not play

i cant solve this problem or error

here is what animation should play

robloxapp-20200415-2033597.wmv (607.1 KB)

but my code does not work its here below

local plr = game.Players.LocalPlayer
repeat wait() until workspace:WaitForChild(plr.Name)

local PlrGui = plr:WaitForChild("PlayerGui")
local PcGui = PlrGui:WaitForChild("PowerGuiPC")
local PCFrame = PcGui:WaitForChild("Frame")
local PowerTextPC = PCFrame:WaitForChild("Power")
local MGui = PlrGui:WaitForChild("PowerGuiMobile")
local MobileControls = PlrGui:WaitForChild("Controls")
local MobileFrame = MGui:WaitForChild("Frame")
local PowerTextMobile = MobileFrame:WaitForChild("Power")


local char = plr.Character
local hum = char:WaitForChild("Humanoid")


local HandleAnim = char:WaitForChild("Handle")
local DribbleAnim = char:WaitForChild("Dribble")
local HandleTrack = hum:LoadAnimation(HandleAnim)
local DribbleTrack = hum:LoadAnimation(DribbleAnim)

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local RepStorage = game:GetService("ReplicatedStorage")
local Events = RepStorage:WaitForChild("Events")
local Equip = Events:WaitForChild("EquipBall")

local PCPlayer = UIS.KeyboardEnabled == true
local MobilePlayer = UIS.TouchEnabled == true

-- Delays
local DribbleDelay = 0.1
local DebounceDelay = 0.2
local BehindDelay = 0.5
--
local http = "http://www.roblox.com/asset/?id="
local Disabled = false

local function Dribble(String, Ball)
	local ScriptsFolder = Ball:WaitForChild("Scripts")
	local AnimationTable = require(ScriptsFolder:WaitForChild("AnimationTable"))
	
	
	
	for i, v in pairs(AnimationTable.Animations) do
		
		
		if i == String then
			
			DribbleTrack:Stop()
			DribbleTrack:Destroy()
			DribbleAnim.AnimationId = http .. v
			DribbleTrack = hum:LoadAnimation(DribbleAnim)
			DribbleTrack:Play()
			
		end
		
		
	end
	
end

local function Handle(String, Ball)
	local ScriptsFolder = Ball:WaitForChild("Scripts")
	local AnimationTable = require(ScriptsFolder:WaitForChild("AnimationTable"))
	
	for i, v in pairs(AnimationTable.Animations) do
		
		DribbleTrack:Stop()
		DribbleTrack:Destroy()
		HandleTrack:Stop()
		HandleTrack:Destroy()
		HandleAnim.AnimationId = http .. v
		HandleTrack = hum:LoadAnimation(HandleAnim)
		HandleTrack:Play()
		
	end
	
end

-- Load Animation

char.ChildAdded:Connect(function(Child)
	
	if Child.Name == "Ball" then
		
		local Ball = Child
		local Events = Ball:WaitForChild("Events")
		local ChangeHand = Events:WaitForChild("ChangeHand")
		local Values = Ball:WaitForChild("Values")
		local Hand = Values:WaitForChild("Hand")
		local ScriptsFolder = Ball:WaitForChild("Scripts")
		local AnimationTable = require(ScriptsFolder:WaitForChild("AnimationTable"))
		
		for _, v in pairs(AnimationTable.Animations) do
			
			
			HandleAnim.AnimationId = http .. v
			hum:LoadAnimation(HandleAnim)
			
			
		end
		

		
	end
	
end)


char.ChildRemoved:Connect(function(Child)
	
	
	if Child.Name == "Ball" then
		
		HandleTrack:Stop()
		DribbleTrack:Stop()
		
		
		
	end
	
	
	
end)


Equip.OnClientEvent:Connect(function(Ball, PowerValue, HandValue)
	
	if PCPlayer then
		
		PcGui.Enabled = true
		PowerTextPC.Text = PowerValue
	elseif MobilePlayer then
		MobileControls.Enabled = true
		MGui.Enabled = true
		PowerTextMobile.Text = PowerValue
	end
	
	--	Update GUI
	if Disabled == false then
		
		
		if char:FindFirstChild("Ball") then
			
			if HandValue == 0 then
				Dribble("RightDribble", Ball)
				
			else
				Dribble("LeftDribble", Ball)
				
				
			end
			
			
		end
		
		
	end
	
	--	IDK
	
	
end)


UIS.InputBegan:Connect(function(input, gameProcessed)
			
			if char:FindFirstChild("Ball") then
				local Ball = char:FindFirstChild("Ball")
				local Events = Ball:WaitForChild("Events")
				local ChangeHand = Events:WaitForChild("ChangeHand")
				local Values = Ball:WaitForChild("Values")
				local Hand = Values:WaitForChild("Hand")
				
				if input.KeyCode == Enum.KeyCode.H or input.KeyCode == Enum.KeyCode.L then
					if Disabled == false then
						Disabled = true
						
						
						if Hand.Value == 0 then
							
							ChangeHand:FireServer(1)
							Dribble("LeftDribble", Ball)
							wait(DribbleDelay)
						elseif Hand.Value == 1 then
							
							ChangeHand:FireServer(0)
							Dribble("RightDribble", Ball)
							wait(DribbleDelay)
						end
					wait(DebounceDelay)
					Disabled = false
				end
		elseif input.KeyCode == Enum.KeyCode.F then
				if Disabled == false then
					
					Disabled = true
					ChangeHand:FireServer(0)
					Handle("FakeCross", Ball) -- the animation shpuld play here but it did not play
					print(Handle("FakeCross")) -- the line of errors
					wait(BehindDelay)
					if char:FindFirstChild("Ball") then
						Dribble("RightDribble", Ball)
						
					end
					wait(DebounceDelay)
					Disabled = false
				end
					
		end
			
	end		
end)	

Module The Animation Script



local AnimationTable = {}

AnimationTable.Animations = {
	
	RightDribble = 04900889894,
	LeftDribble = 04900894178,
	FakeCross = 04900885146,
	
	



}

return AnimationTable

btw i also copied this from a youtube live stream

here’s the link

Where are you getting the error? " [Attempt To Index nil with WaitForChild]"

you meant the line?

30char30char

1 Like

Well I can’t see the line through here so just tell me what the line says

It’s not advised to do this, this is called polling and it’s not efficient, a better way to wait for the character, is like this

local player = game.Players.LocalPlayer
local character = player.Character or player.Characteradded:Wait()
1 Like

see the code with a print(Handle(“FakeCross”))

You can’t print that because [handle] is a function and your not returning anything

Is that in a LocalScript or a Script?

local script

that does the animations

well my animation does not play

You told the script is erroring, right? Reading the above conversation, I couldn’t make it clear which line it is erroring. Could you specify it?

Edit: Nevermind, got it.Could you specify the line of the error attempt to index nil with WaitForChild()?

This is erroring because, at this point, Ball does not exist. According to the if statment, if the ball does not exist, do the dribble stuff, if it does do then FakeCross. Your if statment are wrong.
Here is what’s correct:

local plr = game.Players.LocalPlayer
local character ) plr.Character

local PlrGui = plr:WaitForChild("PlayerGui")
local PcGui = PlrGui:WaitForChild("PowerGuiPC")
local PCFrame = PcGui:WaitForChild("Frame")
local PowerTextPC = PCFrame:WaitForChild("Power")
local MGui = PlrGui:WaitForChild("PowerGuiMobile")
local MobileControls = PlrGui:WaitForChild("Controls")
local MobileFrame = MGui:WaitForChild("Frame")
local PowerTextMobile = MobileFrame:WaitForChild("Power")


local char = plr.Character
local hum = char:WaitForChild("Humanoid")


local HandleAnim = char:WaitForChild("Handle")
local DribbleAnim = char:WaitForChild("Dribble")
local HandleTrack = hum:LoadAnimation(HandleAnim)
local DribbleTrack = hum:LoadAnimation(DribbleAnim)

local UIS = game:GetService("UserInputService")
local CAS = game:GetService("ContextActionService")
local RepStorage = game:GetService("ReplicatedStorage")
local Events = RepStorage:WaitForChild("Events")
local Equip = Events:WaitForChild("EquipBall")

local PCPlayer = UIS.KeyboardEnabled == true
local MobilePlayer = UIS.TouchEnabled == true

-- Delays
local DribbleDelay = 0.1
local DebounceDelay = 0.2
local BehindDelay = 0.5
--
local http = "http://www.roblox.com/asset/?id="
local Disabled = false

local function Dribble(String, Ball)
	local ScriptsFolder = Ball:WaitForChild("Scripts")
	local AnimationTable = require(ScriptsFolder:WaitForChild("AnimationTable"))
	
	
	
	for i, v in pairs(AnimationTable.Animations) do
		
		
		if i == String then
			
			DribbleTrack:Stop()
			DribbleTrack:Destroy()
			DribbleAnim.AnimationId = http .. v
			DribbleTrack = hum:LoadAnimation(DribbleAnim)
			DribbleTrack:Play()
			
		end
		
		
	end
	
end

local function Handle(String, Ball)
	local ScriptsFolder = Ball:WaitForChild("Scripts")
	local AnimationTable = require(ScriptsFolder:WaitForChild("AnimationTable"))
	
	for i, v in pairs(AnimationTable.Animations) do
		
		DribbleTrack:Stop()
		DribbleTrack:Destroy()
		HandleTrack:Stop()
		HandleTrack:Destroy()
		HandleAnim.AnimationId = http .. v
		HandleTrack = hum:LoadAnimation(HandleAnim)
		HandleTrack:Play()
		
	end
	
end

-- Load Animation

char.ChildAdded:Connect(function(Child)
	
	if Child.Name == "Ball" then
		
		local Ball = Child
		local Events = Ball:WaitForChild("Events")
		local ChangeHand = Events:WaitForChild("ChangeHand")
		local Values = Ball:WaitForChild("Values")
		local Hand = Values:WaitForChild("Hand")
		local ScriptsFolder = Ball:WaitForChild("Scripts")
		local AnimationTable = require(ScriptsFolder:WaitForChild("AnimationTable"))
		
		for _, v in pairs(AnimationTable.Animations) do
			
			
			HandleAnim.AnimationId = http .. v
			hum:LoadAnimation(HandleAnim)
			
			
		end
		

		
	end
	
end)


char.ChildRemoved:Connect(function(Child)
	
	
	if Child.Name == "Ball" then
		
		HandleTrack:Stop()
		DribbleTrack:Stop()
		
		
		
	end
	
	
	
end)


Equip.OnClientEvent:Connect(function(Ball, PowerValue, HandValue)
	
	if PCPlayer then
		
		PcGui.Enabled = true
		PowerTextPC.Text = PowerValue
	elseif MobilePlayer then
		MobileControls.Enabled = true
		MGui.Enabled = true
		PowerTextMobile.Text = PowerValue
	end
	
	--	Update GUI
	if Disabled == false then
		
		
		if char:FindFirstChild("Ball") then
			
			if HandValue == 0 then
				Dribble("RightDribble", Ball)
				
			else
				Dribble("LeftDribble", Ball)
				
				
			end
			
			
		end
		
		
	end
	
	--	IDK
	
	
end)


UIS.InputBegan:Connect(function(input, gameProcessed)
			
			if char:FindFirstChild("Ball") then
				local Ball = char:FindFirstChild("Ball")
				local Events = Ball:WaitForChild("Events")
				local ChangeHand = Events:WaitForChild("ChangeHand")
				local Values = Ball:WaitForChild("Values")
				local Hand = Values:WaitForChild("Hand")
				
				if input.KeyCode == Enum.KeyCode.H or input.KeyCode == Enum.KeyCode.L then
					if Disabled == false then
						Disabled = true
						
						
						if Hand.Value == 0 then
							
							ChangeHand:FireServer(1)
							Dribble("LeftDribble", Ball)
							wait(DribbleDelay)
						elseif Hand.Value == 1 then
							
							ChangeHand:FireServer(0)
							Dribble("RightDribble", Ball)
							wait(DribbleDelay)
						end
					wait(DebounceDelay)
					Disabled = false
		     elseif input.KeyCode == Enum.KeyCode.F then
				if Disabled == false then
					
					Disabled = true
					ChangeHand:FireServer(0)
					Handle("FakeCross", Ball) -- the animation shpuld play here but it did not play
					print(Handle("FakeCross")) -- the line of errors
					wait(BehindDelay)
					if char:FindFirstChild("Ball") then
						Dribble("RightDribble", Ball)
						
					end
					wait(DebounceDelay)
					Disabled = false
				end
					
		     end
			
	end		
end)

It seems that this script was just ripped out of the above video, please don’t do that, make your own scripts.

yes it is but i want the animation to play plus the ball exists in the workspace