Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index nil with HumanoidRootPart error?

Asked by 3 years ago

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
0
which line does it say? sne_123456 439 — 3y
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 MarkedTomato 810 — 3y
0
Hi tabbo, thanks for your comment where exactly did you insert the line of code that i sent? sne_123456 439 — 3y
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) TabooAntonioiscool 42 — 3y
View all comments (2 more)
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()!! sne_123456 439 — 3y
0
@TabooAntonioiscool sne_123456 439 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

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!

0
ServerScriptService.RoundSystem:8: attempt to index nil with 'CharacterAppearanceLoaded' is the error i got from that so idek whats happening TabooAntonioiscool 42 — 3y
Ad

Answer this question