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

attempt to index nil with 'Humanoid'????

I am trying to make a toggle animation button. After editing the code for a bit I got the enclosed error. I have been trying to fix it for over 30 minutes but nothing works!! Any reason why?

local userInputService = game:GetService("UserInputService")
local plr = game.Players.LocalPlayer
local char = plr.Character
local name = plr.Name
local summon = true
local Anim = game.Workspace.TheWorldTotality.TWT_PlrPose
local AnimTwo = game.Workspace.TheWorldTotality.TWT_PoseAnim
local stand = game.Workspace:FindFirstChild("TWT")
local loadAnimTwo = char.Humanoid:LoadAnimation(Anim)
local loadAnimOne = stand.Humanoid:LoadAnimation(AnimTwo)
if name == ("Crimsonknightzone") then
	print("Welcome Creator")
	userInputService.InputBegan:Connect(function(input,gameProcessedEvent)
		if input.UserInputType == Enum.UserInputType.Keyboard then
			
			if input.KeyCode == Enum.KeyCode.G then
				if loadAnimOne.IsPlaying== false and loadAnimTwo.IsPlaying == false then
					game.ReplicatedStorage.TWTevents.Pose:FireServer()
					
					loadAnimOne:Play()
					loadAnimTwo:Play()
						
				end
			else
				loadAnimOne:Stop()
				loadAnimTwo:Stop()
			end
		end
	end)
end

4 comments

0

you kinda didnt put everything in a code block...

0

Yeah, i can't help you if I can't put everything in the codeblock. Please code block it.

0

Got it

0

Everything is in the code block

2 answers

Here's something that may help you, I have my notes in here. Please do not ignore my suggestions, considering this is a LocalScript, exploiters may have access to it.

This script may go in StarterPlayer.StarterCharacterScripts or StarterPlayer.StarterPlayerScripts, this will also work in ReplicatedFirst

local UserInputService = game:GetService("UserInputService")
local AnimationIsPlaying = false
local Summon = true
local AnimationTrack -- This is a placeholder for the animation-track.
local AnimationTrack2 -- This is a placeholder for the second animation-track.

local Connection = UserInputService.InputBegan:Connect(function(InputObject,Event)
	if Event == true then -- If the InputObject was processed through the game.
		return
	end
	if InputObject.UserInputState == Enum.UserInputState.Begin and InputObject.KeyCode == Enum.KeyCode.G then
		AnimationIsPlaying = not AnimationIsPlaying
		--[[
		This only works with boolean variables.
		local Variable = not Variable
		It changes false to true, or true to false, depending on what the variable was before.
		]]
		if AnimationIsPlaying == true then
			game:GetService("ReplicatedStorage").TWTevent.Pose:FireServer()
			local Humanoid = game:GetService("Players").LocalPlayer.Character:FindFirstChildWhichIsA("Humanoid")
			AnimationTrack = Humanoid:LoadAnimation(workspace["TheWorldTotality"].TWT_PlrPose)
			AnimationTrack:Play()
			AnimationTrack2 = Humanoid:LoadAnimation(workspace["TheWorldTotality"].TWT_PoseAnim)
			AnimationTrack2:Play()
		else
			pcall(AnimationTrack.Stop,AnimationTrack)
			pcall(AnimationTrack2.Stop,AnimationTrack2)
		end
	end
end)

if game:GetService("Players").LocalPlayer.UserId ~= (1234567890) then
	Connection:Disconnect()
	--[[
	Replace the UserId with yours, UserId is much more secure.
	Not sure how? It's in your profile link.
	]]
	pcall(script.Destroy,script) -- It's good to destroy the script so nobody can decompile it with exploits.
end

That can happen because the script ran before the player's character was added, and to fix that, you can either:

  1. Move the script to StarterCharacterScripts

  2. Change plr.Character to plr.CharacterAdded:Wait()

Try one of the two methods and let me know if something changed

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