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

Getting error {attempt to index nil with "Humanoid"} in script, how do I fix it? Thanks!

I'm currently making a swordfight game as a newbie to scripting in Roblox, with the help of AlvinBlox's videos.

In the script attached, it's supposed to update the Status GUI (The textlabel on top of the screen) to say a person killed another person immediately, when someone dies.

But instead, the Status GUI only updates when the dead person respawns.

Here is a link to a screenshot I took of the output window when the error occurs (afaik there are no other errors): https://media.discordapp.net/attachments/484292855880351744/672380173332512769/unknown.png?width=1192&height=671

The script is called "Stats", by the way.

Here's the script:

game.Players.PlayerAdded:Connect(function(player)
	
	local leaderstats = Instance.new("Folder")
	leaderstats.Name = "leaderstats"
	leaderstats.Parent = player
	
	local bucks = Instance.new("IntValue")
	bucks.Name = "Bucks"
	bucks.Value = 0
	bucks.Parent = leaderstats
	
	player.CharacterAdded:Connect(function(character)
		
		character.Humanoid.Died:Connect(function(character)
			
			--Whenever somebody dies, this event will run
			if character.Humanoid and character.Humanoid:FindFirstChild("creator") then
				game.ReplicatedStorage.Status.Value = tostring(character.Humanoid.creator.Value).." KILLED "..player.Name
			end
			
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy()
			end
			
			player:LoadCharacter()
			
		end)
		
	end)
end)

The creator tag you see in line 17 and so on is from the official ROBLOX sword model, and creator.Value for every player A is the name of the player who killed A.

If the MainScript is useful, here it is as well:

-- Definition of variables

local ReplicatedStorage = game:GetService("ReplicatedStorage")
local ServerStorage = game:GetService("ServerStorage")
local MapsFolder = ServerStorage:WaitForChild("Maps")
local Status = ReplicatedStorage:WaitForChild("Status")
local GameLength = 90 -- In seconds
local reward = 10 -- Number of bucks rewarded to each winner

-- Game Looping

while true do
	
	Status.Value = "Waiting for enough players to join"
	repeat wait(1) until game.Players.NumPlayers >= 2
	Status.Value = "Intermission, we now have enough players in the game"
	wait(10)
	
	local players = {}
	
	--Adding players into the players table
	for i, player in pairs(game.Players:GetPlayers()) do
		if player then
			table.insert(players,player)
		end
	end
	
	wait(2)
	------------------------------------
	
	--Choosing a random map
	local AvailableMaps = MapsFolder:GetChildren()
	local ChosenMap = AvailableMaps[math.random(1,#AvailableMaps)]
	Status.Value = "The "..ChosenMap.Name.." Map was Chosen!"
	
	--Putting the chosen map into the workspace
	local ClonedMap = ChosenMap:Clone()
	ClonedMap.Parent = workspace
	
	--Teleporting players to chosen map
	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")
	if not SpawnPoints then
		print("Spawnpoints not found!")
	end
	
	local AvailableSpawnPoints = SpawnPoints:GetChildren()
	
	for i, player in pairs(players) do
		if player then
			character = player.Character
			
			if character then
				
				--Teleporting them
				character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame
				table.remove(AvailableSpawnPoints,1)
				
				--Giving them a sword
				local Sword = ServerStorage.Sword:Clone()
				Sword.Parent = player.Backpack
				
				local GameTag = Instance.new("BoolValue")
				GameTag.Name = "GameTag"
				GameTag.Parent = player.Character
				
				
			else
				
				--If there's no character
				if not player then
					table.remove(players,i)
				end
				
			end
		end
	end
	
	Status.Value = "Get Ready to Play!"
	wait(2)
	
	-- Main game loop
	for i = GameLength, 0, -1 do
		
		for x, player in pairs(players) do
			
			if player then
				character = player.Character
				
				if not character then
					--Player has left the game or something else happened to their character
					
				else
					if character:FindFirstChild("GameTag") then
						
						--They are still alive
						print(player.Name.." is still in the game.")
						
					else 
						
						--They are dead
						table.remove(players,x)
						print(player.Name.." has been removed from the game.")
						
					end
				end
			else
				
				table.remove(players,x)
				print(player.Name.." has been removed from the game.")
				
			end
		end
		
		Status.Value = "There are "..i.." seconds remaining, and "..#players.." players are left."
		
		if #players == 1 then
			--Only one player is standing!
			Status.Value = "The winner is "..players[1].Name.."!"
			players[1].leaderstats.Bucks.Value = players[1].leaderstats.Bucks.Value + reward
			break
			--The game is over!
			
		elseif #players ==0 then
			--No one is left!
			Status.Value = "Nobody won!"
			break
			--The game is over!
			
		elseif i == 0 then
			--Timer runs out to 0!
			Status.Value = "Time's up!"
			break
			--The game is over!
			
		end
		
		wait(1)
	end
	
	print("Round over!")
	for i, player in pairs(game.Players:GetPlayers()) do
		character = player.Character
		
		if not character then
			--Ignore them
		else
			
			if character:FindFirstChild("GameTag") then
				character.GameTag:Destroy() --Remove their gametags
			end
			
			if player.Backpack:FindFirstChild("Sword") then
				player.Backpack.Sword:Destroy() --Remove the sword from their inventories
			end
			
			if character:FindFirstChild("Sword") then
				character.Sword:Destroy() --Remove the sword from their inventories
			end
			
		end 
		
		player:LoadCharacter()
		
	end
	
	ClonedMap:Destroy()
	wait(2)
	Status.Value = "Round over, game ended!"
	
end

Any help is welcomed and thank you so much for taking the time to read this post!

print("From the stars,\nRyan.")

1 comment

0

add a wait(.1) before character.Humanoid.Died

1 answer

Try removing the if humanoid thing on line 17.

The script would be:

game.Players.PlayerAdded:Connect(function(player)

    local leaderstats = Instance.new("Folder")
    leaderstats.Name = "leaderstats"
    leaderstats.Parent = player

    local bucks = Instance.new("IntValue")
    bucks.Name = "Bucks"
    bucks.Value = 0
    bucks.Parent = leaderstats

    player.CharacterAdded:Connect(function(character)

        character.Humanoid.Died:Connect(function(character)

            --Whenever somebody dies, this event will run
            if character.Humanoid:FindFirstChild("creator") then
                game.ReplicatedStorage.Status.Value = tostring(character.Humanoid.creator.Value).." KILLED "..player.Name
            end

            if character:FindFirstChild("GameTag") then
                character.GameTag:Destroy()
            end

            player:LoadCharacter()

        end)

    end)
end)

I am not sure if it will work but same kinds of problems have been fixed by this.

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