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

attempt to index nil with 'HumanoidRootPart' ?

Asked by 2 years ago

I Want players in a table to teleport in a specific position. But, the output says that HumanoidRootPart Is Nil.

And I have made a table and saw,

[1] = Brioche_Noodle [2] = "*** cycle table reference detected ***"

Function That I am focusing on,

function spawnMap()
    local Maps = Storage.Maps:GetChildren()
    chosenMap = Maps[math.random(1, #Maps)]

    chosenMap:Clone()
    chosenMap.Parent = workspace.Map


    for _, player in ipairs(Playing) do
        if player then
            print(player)
            player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
        end
    end

end

Here is my whole script.

local Players = game:GetService("Players")
local Teams = game:GetService("Teams")
local Storage = game:GetService("ServerStorage")

local Playing = {}
local NotPlaying = {}
local Infected = {}

local Team = {
    Lobby = Teams.Lobby,
    Infected = Teams.Infected,
    Citizens = Teams.Citizens
}
local gameStarted = false
local chosenMap

local function onPlayerAdded(player)
    print(player.Name .. " || Joined || ")

    if gameStarted == false then 
        print(player.Name .. " is added to The Playing Table")

        table.insert(Playing, player)
        print(Playing)
    else
        print(player.Name .. " is added to The Not Playing Table")

        table.insert(NotPlaying, player)
        print(NotPlaying)
    end
    player.Team = Team.Lobby
end



local function addPlayerToPlaying()
    local AllPlayers = Players:GetPlayers()

    table.insert(Playing, Playing)
end

local function addPlayerToNotPlaying()
    local AllPlayers = Players:GetPlayers()

    table.insert(NotPlaying, Playing)
end

function spawnMap()
    local Maps = Storage.Maps:GetChildren()
    chosenMap = Maps[math.random(1, #Maps)]

    chosenMap:Clone()
    chosenMap.Parent = workspace.Map


    for _, player in ipairs(Playing) do
        if player then
            print(player)
            player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
        end
    end

end

function despawnMap()
    local Maps = Storage.Maps:GetChildren()
    chosenMap.Parent = nil

    for _, player in ipairs(Playing) do
        if player then
            player.CharacterAdded:Connect(function(Character)
                Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Lobby.Teleporter.Position)
            end)
        end
    end

end

-- Listeners 

Players.PlayerAdded:Connect(onPlayerAdded)

-- Script

while true do
    gameStarted = false 

    wait(4)

    gameStarted = true
    addPlayerToPlaying()
    spawnMap()
    wait(10)
    despawnMap()

    Playing = nil
    NotPlaying = nil
    Infected = nil
end

0
addPlayerToPlaying/NotPlaying does not do as their title says. Why are you inserting the Playing array?  Ziffixture 6913 — 2y
0
FYI, line 57/70 is redundant. Ziffixture 6913 — 2y
0
despawnMap fails to live up to its title, too Ziffixture 6913 — 2y

2 answers

Log in to vote
1
Answered by 2 years ago

Change your spawnMap() function to the following:

function spawnMap()
    local Maps = Storage.Maps:GetChildren()
    chosenMap = Maps[math.random(1, #Maps)]

    chosenMap:Clone()
    chosenMap.Parent = workspace.Map


    for _, player in ipairs(Playing) do
        if player then
            print(player.Name.." is in Playing table")
            local char = player.Character or player.CharacterAdded:Wait()
            if char then
                print(player.Name.."'s character exists")
                player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
            end
        end
    end
end

I tested the script in my own place, so this should work. If you still get the same (or a different) error, get back to me and I'll see if I can help more.

0
It's best to ignore the player if they're dead. Sending them dead to the game or waiting 'til they respawn can/will yield undesirable results. Ziffixture 6913 — 2y
0
You're also building a Vector3 from a Vector3? Doing so will result in 0, 0, 0. Ziffixture 6913 — 2y
0
I've changed the, HumanoidRootPart.Position = Vector3.new() into, HumanoidRootPart.CFrame = CFrame.New() And It Works. But, the Output Says that :Wait() Is Nil. Brioche_Noodle 45 — 2y
0
I've changed the, HumanoidRootPart.Position = Vector3.new() into, HumanoidRootPart.CFrame = CFrame.New() And It Works. But, the Output Says that :Wait() Is Nil. Brioche_Noodle 45 — 2y
View all comments (2 more)
0
I removed the Charcter added And It worked! Brioche_Noodle 45 — 2y
0
Ah good to hear. JB_SuperGamer 165 — 2y
Ad
Log in to vote
0
Answered by
Vlym 19
2 years ago

You need to check the player's character still exists as well as the player

Answer this question