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 HumanoidRootPart error?

I'm working on a round system, and I need the script to wait until the character has loaded until it continues but I don't know how. Anyone got a fix?

Here's the script:

local lobbylocation = workspace.Lobby.Lobby.Position + Vector3.new(0, 3, 0)
local gamelocation = workspace.Maps.Main.Position + Vector3.new(0, 3, 0)
local replicatedstorage = game:GetService("ReplicatedStorage")
local timeevent = replicatedstorage:WaitForChild("TimeEvent")
local propcount = replicatedstorage.PropCount
local timeAmount = 20

game.Players.PlayerAdded:Connect(function(plr)
	plr.Team = game.Teams.Lobby
	replicatedstorage.ServerCount.Value += 1
end)

game.Players.PlayerRemoving:Connect(function()
	replicatedstorage.ServerCount.Value -= 1
end)

wait(1)

local function playgame()--300
	local timerText = "Time Left: "
	while timeAmount > 0 do
		timeevent:FireAllClients(timeAmount, timerText)
		wait(1)
		timeAmount -= 1
	end
end


local function playintermission()
	local intermission = 10--60
	local timerText = "Intermission: "
	while intermission > 0 do
		timeevent:FireAllClients(intermission, timerText)
		wait(1)
		intermission -= 1
	end
end

local function resetplayers()
	for _, plr in pairs (game.Players:GetChildren()) do
		plr.Character.HumanoidRootPart.CFrame = CFrame.new(lobbylocation)
	end
end



local function teleportplayers()
	for _, plr in pairs (game.Players:GetChildren()) do
		local huntercount = replicatedstorage:WaitForChild("HunterCount")
		if huntercount.Value <= 0 then
			huntercount.Value += 1
			plr.Character.PlayerTeam.Value = "Hunters"
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(gamelocation)
			plr.Team = game.Teams.Hunters
		else
			propcount.Value += 1
			plr.Character.PlayerTeam.Value = "Props"
			plr.Character.HumanoidRootPart.CFrame = CFrame.new(gamelocation)
			plr.Team = game.Teams.Props
		end

	end
end

local function resetall()
	for _, plr in pairs(game.Players:GetChildren()) do
		local huntercount = replicatedstorage:WaitForChild("HunterCount")
		huntercount.Value = 0
		propcount.Value = 0
		plr.Character.Humanoid.Health = 0
		plr.Team = game.Teams.Lobby
		if plr.Team == game.Teams.Hunters then
			local propgun = plr:FindFirstChild("PropGun") or plr.Character:FindFirstChild("PropGun")
			if propgun then
				propgun:Destroy()
			end
		end
	end
end


local function hunterwait()
	for _, plr in pairs (game.Players:GetChildren()) do
		if plr.Team == game.Teams.Hunters then
			local hunterscreen = plr.PlayerGui:WaitForChild("HunterScreen")
			hunterscreen.Enabled = true
			wait(5)
			hunterscreen.Enabled = false
			local propgun = replicatedstorage:WaitForChild("PropGun")
			local gunclone = propgun:Clone()
			gunclone.Parent = plr.Backpack
		end
	end
end



while wait() do
	if replicatedstorage.ServerCount.Value >= 1 then
		resetplayers()
		playintermission()
		teleportplayers()
		hunterwait()
		if propcount.Value == 0 then
			print("HUNTERS WIN")
			resetall()
		end

		playgame()
		if propcount.Value == 0 then
			print("HUNTERS WIN")
			resetall()

		else if timeAmount == 0 then
				print("PROPS WIN")
				resetall()
			end
		end
	end
end

6 comments

0

which line does it say?

0

You can probably avoid this and use Player.RespawnLocation and Player:LoadCharacter() so you don't have to mess around with positions but for this you would need spawnlocations in your map

0

Hi tabbo, thanks for your comment where exactly did you insert the line of code that i sent?

0

Right at the top, before all the functions etc. - I've fixed it just by adding a simple wait(2) (Probably not the most ideal way but it works)

0

Hi there I have edited my code, sorry i just forgot 1 line, i hope it works now, its way more efficient than wait()!!

0

@TabooAntonioiscool

1 answer

If you want the character to fully load you should use the CharacterAppearenceLoaded event. This checks if the whole appearence of the character is loaded, meaning that the character is also fully loaded.

Here's an example

local function playerAdded(player)
	player.CharacterAppearanceLoaded:Connect(function(character)
          local hum = character:WaitForChild("Humanoid")
          if hum then
            print("The character is fully loaded")
          end
     end
end

Players.PlayerAdded:Connect(playerAdded)

Not that i havent tested it, but it should work

Hope this was helpful!

Any questions? Just ask!

1 comment

0

ServerScriptService.RoundSystem:8: attempt to index nil with 'CharacterAppearanceLoaded' is the error i got from that so idek whats happening

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