ServerScriptService.Round:17: attempt to index a nil value

I’m new to coding so if I made really dumb mistakes, forgive me.

I’m trying to make a “Plates of Fate” type game with combat n stuff but the script I use doesn’t teleport all the players. Here’s the code,

while true do
repeat wait() until game.Players.NumPlayers >= 1
wait(2)
players = game.Players:GetPlayers()
i = 1
repeat
local Plate = game.ServerStorage.Cloud -- Change this to distinguish what the target block/model is.
local CPlate = Plate:Clone()

x = math.random(-256,256)
y = math.random(-10,10)
z = math.random(-256,256)
CPlate.Parent = game.Workspace
CPlate.Position = Vector3.new(x,y,z)

workspace:FindFirstChild(players[i].Name).Torso.CFrame = CFrame.new(x,y+2,z)
i = i+1
until i == #players + 1
end
1 Like

If your characters are R15, Torso won’t exist. Replace that with HumanoidRootPart since it exists regardless of the character type.

Also, you can format code on the devforum like this:

--test
print("hi")

And here’s what it looks like for me (just add three tick marks on the top and bottom with “lua” at the top)
image

1 Like

No, character type is set to R6, I can link the file to make it easy to find the bug.

PoFB World.rbxl (15.2 KB)

[Btw thanks for the format code thing]

(Talking about the code in the placefile he sent)

Firstly, please indent your code properly. If you don’t indent it correctly, it will look horrible and no scripter will want to help.

I’m getting no errors, and there is no indexing on line 17. It looks like you posted an old error to me.

(Oops sorry for replying to you, Alvin)

I tried it, i didn’t get any error.

It breaks only when there is more than one player, you need more than one player to start ^^

2 Likes

I made a local two player server and still no errors. Maybe I need more
Ok error is on line 16 like I suspected

Welcome to the Dev Forum!

On line 19, if you remove the + 1 it seems to be fine. I think it’s trying to find an extra player after the last one. You could alternatively use a for loop to loop through all players in the game:

for i, v in pairs(players) do
        local Plate = game.ServerStorage.Cloud -- Change this to distinguish what the target block/model is.
		local CPlate = Plate:Clone()
		
		x = math.random(-256,256)
		y = math.random(-10,10)
		z = math.random(-256,256)
		CPlate.Parent = game.Workspace
		CPlate.Position = Vector3.new(x,y,z)
		
		v.Character.Torso.CFrame = CFrame.new(x,y+2,z)
end

I also suggest you add a longer wait() at the top of your script to allow for all players to be in by the time the game starts and a delay after you teleport players so that they aren’t repeatedly teleported, unless that’s how your game is going to work.

5 Likes

Thanks for the help, and I’ll add a longer wait when I finish my round system.

1 Like