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

"attempt to index nil with 'CFrame'" but I am referencing a part. Any fixes?

Asked by 4 years ago
Edited 4 years ago

So I was debugging my script because I found countless others, I thought I hit the last one until this one came.

Workspace.mc:55: attempt to index nil with 'CFrame'

Here is the line of code giving me trouble.

player.Character.HumanoidRootPart.CFrame = game.Workspace.CurrentMap:WaitForChild("map_1", 7.5).CFrame + Vector3.new(0, 6, 0)

No, it is not the player.Character part. I added and deleted some stuff earlier to make sure it's the second part. Yes, the path is correct and it is a part, not a model.

Here is the function with the error:

function teleportPlayers()
    local t1 = true
    local t2 = false

    for i, player in pairs(game:GetService'Players':GetPlayers'') do
        if t1 == true then
            player.TeamColor = game.Teams.defend.TeamColor
            t1 = false
            t2 = true
            player.Character.HumanoidRootPart.CFrame = game.Workspace.CurrentMap:WaitForChild("map_1", 7.5).CFrame + Vector3.new(0, 6, 0)
        else
            player.TeamColor = game.Teams.attack.TeamColor
            t1 = true
            t2 = false
            player.Character.HumanoidRootPart.CFrame = game.Workspace.CurrentMap:WaitForChild("map_2", 7.5).CFrame + Vector3.new(0, 6, 0)
        end
    end
end

If needed, here is all of the code:

local players = game.Players:GetChildren()
local maps = game.Lighting:GetChildren()
local currentmap = game.Workspace:WaitForChild("CurrentMap")
local chosenmap = script:WaitForChild("ChosenMap")
local spawner1
local spawner2
local choices = {}
local status = game.ReplicatedStorage:WaitForChild("status")
local intermission


--choose map randomly
function chooseMap()
    for i = 1,#maps do
        if maps[i]:isA("Model") then
            table.insert(choices,maps[i])
        end
    end
    local picked = math.random(1,#maps)
    chosenmap.Value = tostring(picked)
end

--load map
function loadMap()
    local map = game.Lighting.Maps:FindFirstChild("map" .. chosenmap.Value)
    local mapnamelol = ("map" .. chosenmap.Value)
    print(mapnamelol)
    map:Clone().Parent = currentmap
end

--unload and delete
function unloadMap()
    for i,v in pairs(currentmap:GetChildren()) do
        if v:isA("Model") then
            v:Destroy()
        end
    end
end


local mapqw = game.Lighting.Maps:FindFirstChild("map" .. chosenmap.Value)

--tp
function teleportPlayers()
    local t1 = true
    local t2 = false

    for i, player in pairs(game:GetService'Players':GetPlayers'') do
        if t1 == true then
            player.TeamColor = game.Teams.defend.TeamColor
            t1 = false
            t2 = true
            player.Character.HumanoidRootPart.CFrame = game.Workspace.CurrentMap:WaitForChild("map_1", 7.5).CFrame + Vector3.new(0, 6, 0)
        else
            player.TeamColor = game.Teams.attack.TeamColor
            t1 = true
            t2 = false
            player.Character.HumanoidRootPart.CFrame = game.Workspace.CurrentMap:WaitForChild("map_2", 7.5).CFrame + Vector3.new(0, 6, 0)
        end
    end
end
--tp back
function teleportBack()
    for i,player in pairs(players) do
        player.TeamColor = game.Teams.Spectator.TeamColor
        player:LoadCharacter()
    end
end

--cycle it
while true do
    intermission = 10
    while intermission >= 0 do
        status.Value = ("Intermission (" .. intermission .. ")")
        wait(1)
        intermission = intermission - 1
    end
    wait(3)
    status.Value = ("")
    chooseMap()
    status.Value = ("Map: " .. chosenmap.Value)
    loadMap()
    status.Value = ("Loading Map")
    wait(1)
    teleportPlayers()
    wait(10)
    status.Value = ("Unloading Map")
    unloadMap()
    teleportBack()
    status.Value = ("Teleported Back")
end

Any help is appriciated.

0
I can tell from the error that, for some reason, it's failing to find "map_1" within game.Workspace.CurrentMap, because it says that your trying to find a CFrame from a nil value. That's all I can say about it. I'd rather not look through all of that code, cause it would probably take a good 10+ minutes to interpret and fix the code. It'd be better for you to look at it yourself sheepposu 561 — 4y
0
If you still can't find the problem, then source the main root of the problem and then send that so that the code snippet is shorter and more people will be willing to help sheepposu 561 — 4y
0
Okay, I'll edit the code block to only that function. Thank you. lukasdim -3 — 4y
0
Also, I double checked and yes, "map_1" is inside the directory I provided. lukasdim -3 — 4y

1 answer

Log in to vote
0
Answered by 4 years ago

Completely my mistake, but I did not realize that WaitForChild and FindFirstChild only works for immediate children.

Ad

Answer this question