Error: Attempt to index nil with 'Character'

I am attempting to play an animation that contains a welded basketball. The basketball is on the court, when a player touches it, it welds to their RightHand (R15) and should play the animation. Upon testing, it turned out that the ball welded correctly, but the animation did not play. Any ideas on how to modify the script or anything in the explorer for it to work? Thanks!

Script:

script.Parent.Touched:Connect(function(Hit)
	
	if script.Parent.Parent.Parent.Parent.StarterPlayer.StarterCharacter:FindFirstChild("RightHand") and not script.Parent.Parent.Parent.Parent.StarterPlayer.StarterCharacter:FindFirstChild(script.Parent.Name) then
		
		if script.Parent.CanPickup.Value == true then
			
			script.Parent.CanPickup.Value = false
			
			script.Parent.CanCollide = false

			local RightHand = script.Parent.Parent.Parent.Parent.StarterPlayer.StarterCharacter:FindFirstChild("RightHand")

			script.Parent.Motor6D.Part0 = RightHand
			script.Parent.Motor6D.Part1 = script.Parent

			script.Parent.Parent = RightHand.Parent

			RightHand.Parent.PickedUpBall.Value = true
			
			local anim = game.StarterPlayer.StarterCharacter.Animations.DribbleR_Final
			local player = game.Players.LocalPlayer
			local char = player.Character
			local hum = char.Humanoid
			
			local animTrack = hum:LoadAnimation(anim)
			animTrack:Play()
			
		end
		
	end
	
end)

Explorer:
image

image

Note: All scripts in StarterCharacterScripts are disabled.

1 Like

which line is the error in ?

Is this a Script or a LocalScript? Your error lies in that Scripts cannot get game.Players.LocalPlayer, only LocalScripts can.

The use of StarterPlayer.StarterCharacter:FindFirstChild(“RightHand”) is also confusing. This will not get a real character’s hand, maybe you want Hit.Parent:FindFirstChild("RightHand")?

Line 22: local char = player.Character

For some reason with the local script, the ball doesn’t weld to the character and the animation doesn’t play. Although, this got rid of the error.

If its a LocalScript then use

-- Waits for the character to load in
local char = Player.Character or Player.CharacterAdded:Wait()