Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

Attempt to index nil with 'CFrame' ?

Ok this has been confusing me for days. So the round script I made works for a few rounds then completley breaks, it says the error is in here, why does it break after a few rounds?

	local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

	if not SpawnPoints then
		print("Spawn points not found")
	end

	local AvailableSpawnPoints = SpawnPoints:GetChildren()	
	
	for i, player in pairs(game.Players:GetChildren()) do
		if player then
			character = player.Character

			if character then
					character:FindFirstChild("HumanoidRootPart").CFrame = AvailableSpawnPoints[1].CFrame + Vector3.new(0,5,0)
					table.remove(AvailableSpawnPoints,1)
				end
			end
		end

This is in a while true do loop

Cloned map is the map that was randomply chosen, and spawn points is a model inside the cloned map were players will be teleported to. It chosen one of the few spawn points then teleports the player there. (Once again, it works for a few rounds then breaks)

Whats wrong?

2 comments

0

what line was that error on?

0

You slowly remove spawn points on line 15 and in this script you are not adding them back, the table just runs out of spawn points.

2 answers

✓ Accepted answer

Your problem is that you are never clearing the table. I'm assuming that you change maps every round and delete the previous map. This means that when you try to use the values in the table, you will try to index a spawn that no longer exists. This is a pretty easy fix, lets just create a function that clears the table for us and call it at the end of each round.

local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

local function clearTable(tbl) --making our function
	for i = #tbl, 1, -1 do
		table.remove(tbl, i)
	end
end

Pretty simple right. We just run a loop that counts down from the number of spawns in the table and removes the values all the way to 1. Next we just have to call the function at the end of the loop and provide it with the table we want to clear.

if not SpawnPoints then
    print("Spawn points not found")
end

local AvailableSpawnPoints = SpawnPoints:GetChildren()  

for i, player in pairs(game.Players:GetChildren()) do
    if player then
        character = player.Character

        if character then
                character:FindFirstChild("HumanoidRootPart").CFrame = SpawnPoints:FindFirstChild(random).CFrame + Vector3.new(0,5,0)
                table.remove(AvailableSpawnPoints,1)
            end
        end
    end
    clearTable(AvailableSpawnPoints)

I'm unsure if where I called the function is correct without seeing the rest of the script, but you should be able to experiment with it. Let me know if it doesn't work or if you have any questions. Hope I helped!

The reason for this error is that index values doesn't work with GetChildren() like you included in line 14. If you want to spawn in a specific one, then you have to do like AvailableSpawnPoints[name of spawn] . Also, numbers cannot be names. You can name like Spawn1 or Spawn2 but not entirely a number.

Or, if you want to spawn in a random location, you can do like:

local SpawnPoints = ClonedMap:FindFirstChild("SpawnPoints")

if not SpawnPoints then
    print("Spawn points not found")
end

local AvailableSpawnPoints = SpawnPoints:GetChildren()  

for i, player in pairs(game.Players:GetChildren()) do
    if player then
        character = player.Character

        if character then
                character:FindFirstChild("HumanoidRootPart").CFrame = SpawnPoints:FindFirstChild(random).CFrame + Vector3.new(0,5,0)
                table.remove(AvailableSpawnPoints,1)
            end
        end
    end

1 comment

0

Actually what he is doing with numbers is fine, using AvailableSpawnPoints[1] will get the first value in that table.

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