A weird glich, can't figure it out!

The code below is a section of a bigger script, but this breaks sometimes but not all the time. It is a main scripts and is teleporting the players. However, on the bolded line the error pops up sometimes but not all the time. Can anyone spot an error! Thanks…

local redSpawn = mapChosenClone.RedSpawns:GetChildren()
local blueSpawn = mapChosenClone.BlueSpawns:GetChildren()
for i,v in pairs(game.Players:GetPlayers()) do
name = v.Name
check = game.Workspace:FindFirstChild(name)
if check then
local checkHumanoid = check:FindFirstChild(‘Humanoid’)
if check.Stats.Team.Value == ‘R’ and checkHumanoid then
check:MoveTo(redSpawn[i].Position)
elseif check.Stats.Team.Value == ‘B’ and checkHumanoid then
check:MoveTo(blueSpawn[i].Position) --THIS LINE HERE
else
print(‘No team’)
end
end
end

I might not be able to help, but it would help to know what kind of error you’re getting from the output.

Errors arent just for the line number. They contain important information. Post your output. :neutral_face:

2 Likes

There is a nil value on the index

He means post the stacktrace and the error (red followed by blue text)

If you’re getting a nil value, then that probably means there is something in that line that the system can’t find. If the 2 lines above works fine, with almost identical wording, I’d assume it has something to do with the actual variable. Check to see if “blueSpawn” exists at the location it needs to be in.

I’m not an expert, so I can’t actually fish out specific errors in code, but based on my assumptions there is probably something missing. Check your spelling too! That’s really important, Caps, everything.

1 Like

Try this. There may be typos.

local redSpawn = mapChosenClone.RedSpawns:GetChildren()
local blueSpawn = mapChosenClone.BlueSpawns:GetChildren()
local numRedSpawns = #redSpawn
local numBlueSpawns = #blueSpawn
local numRed = 0
local numBlue = 0
for i, v in pairs(game.Players:GetPlayers()) do
local char = v.Character
if char then
local tv = char:WaitForChild("Stats"):WaitForChild("Team").Value
if tv == "R" then
numRed = (numRed % numRedSpawns) + 1
char:WaitForChild("HumanoidRootPart").CFrame = redSpawn[numRed].CFrame
elseif tv == "B" then
numBlue = (numBlue % numBlueSpawns) + 1
char:WaitForChild("HumanoidRootPart").CFrame = blueSpawn[numBlue].CFrame
end
end
end

Please try to put your code into a coding snippet by putting these 3 letters before and after where your code is located: ```

Example: `` ` testCode = “test” ```

testCode = "test"

This makes it easier for us to read and help.

Doing Bluespawns[i] doesn’t need a numbered index. Bluespawns[number] will return whatever is in the position of number.
I. E.

Table = {“hey”, “there”, “people”}
print(Table[2]) —will return “there”

edit: just gotta love how iOS formats two dashes to —, instead of actually two dashes.

you are doing a function on a Character Model, which as what i remembers that does not have any PrimaryPart while :MoveTo() needs one. So instead you may wanna run a HumanoidRootPart check after Character