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,

01function spawnMap()
02    local Maps = Storage.Maps:GetChildren()
03    chosenMap = Maps[math.random(1, #Maps)]
04 
05    chosenMap:Clone()
06    chosenMap.Parent = workspace.Map
07 
08 
09    for _, player in ipairs(Playing) do
10        if player then
11            print(player)
12            player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
13        end
14    end
15 
16end

Here is my whole script.

01local Players = game:GetService("Players")
02local Teams = game:GetService("Teams")
03local Storage = game:GetService("ServerStorage")
04 
05local Playing = {}
06local NotPlaying = {}
07local Infected = {}
08 
09local Team = {
10    Lobby = Teams.Lobby,
11    Infected = Teams.Infected,
12    Citizens = Teams.Citizens
13}
14local gameStarted = false
15local chosenMap
View all 99 lines...
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:

01function spawnMap()
02    local Maps = Storage.Maps:GetChildren()
03    chosenMap = Maps[math.random(1, #Maps)]
04 
05    chosenMap:Clone()
06    chosenMap.Parent = workspace.Map
07 
08 
09    for _, player in ipairs(Playing) do
10        if player then
11            print(player.Name.." is in Playing table")
12            local char = player.Character or player.CharacterAdded:Wait()
13            if char then
14                print(player.Name.."'s character exists")
15                player.Character.HumanoidRootPart.Position = Vector3.new(chosenMap.Teleporter.Position)
16            end
17        end
18    end
19end

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