Attempt to index nil with 'UpperTorso'

So I am making a zombie games and I have 3 different maps and I am trying to make the maps randomly spawn but I keep getting an error. Here is the script with the error.

local countdown = game.ReplicatedStorage.Values:WaitForChild(“countdown”)

for i = 15,0,-1 do
countdown.Value = i
if 1 then
local map = math.random(1,3)
if map == 1 then
game.ReplicatedStorage.Maps[“Nuketown Zombies”]:Clone().Parent = workspace.Map
end
if map == 2 then
game.ReplicatedStorage.Maps[“Rundown hosipital area”]:Clone().Parent = workspace.Map
end
if map == 3 then
game.ReplicatedStorage.Maps[“Town Map”]:Clone().Parent = workspace.Map
end
print(map)
for i, v in pairs(game.Players:GetPlayers()) do
v.Character.UpperTorso.CFrame = workspace.Map:FindFirstChildOfClass(“Model”).Spawn.CFrame
end
end
wait(1)
end

And the error
18:09:11.869 2 - Server - Script:16
18:09:11.870 ServerScriptService.Script:18: attempt to index nil with ‘UpperTorso’ - Server - Script:18
18:09:11.870 Stack Begin - Studio
18:09:11.870 Script ‘ServerScriptService.Script’, Line 18 - Studio - Script:18
18:09:11.870 Stack End

Any help is appreciated.
Thanks

Does the player rig is in R6 mode? If so, change it to Torso

I think you should either do a waitforchild, Example:

Character:WaitForChild("UpperTorso")

or you could use the HumanoidRootPart as the part being teleported. Example:

Character:WaitForChild("HumanoidRootPart")

I will change it to torso but still errors

It still will error with both at the WaitForChild

Is the script on server side or local side?

It’s a regular script in serverscript service I was thinking of changing to a localscript but I did not know if it would change anything or not

Ok, since you’re using a server script, then you should do

if not v.Character then return end

That should be able to get the character on a serverside if it hasn’t loaded in yet.

I did but it still errors at WaitForChild

18:38:27.193 3 - Server - Script:16
18:38:27.193 ServerScriptService.Script:18: attempt to index nil with ‘WaitForChild’ - Server - Script:18
18:38:27.193 Stack Begin - Studio
18:38:27.193 Script ‘ServerScriptService.Script’, Line 18 - Studio - Script:18
18:38:27.194 Stack End

Just take out the waitforchild and try it out.

Now it errored how it did at the beginning
18:41:13.529 2 - Server - Script:16
18:41:13.530 ServerScriptService.Script:18: attempt to index nil with ‘UpperTorso’ - Server - Script:18
18:41:13.530 Stack Begin - Studio
18:41:13.530 Script ‘ServerScriptService.Script’, Line 18 - Studio - Script:18
18:41:13.530 Stack End

where did you put the

if not v.Character then return end

?

like this?

	for i, v in pairs(game.Players:GetPlayers()) do
		v.Character.UpperTorso.CFrame = workspace.Map:FindFirstChildOfClass("Model").Spawn.CFrame
		if not v.Character then return 
		end
	end
end
wait(1)

end

This caused by either the rig type the player loading in bears is R6 or because your code is executing before UpperTorso instantiates.

To fix this you can just add a slight pause before the code runs or call :WaitForChild.

Thank you been stuck on it for a while I solved it by adding a wait(1) right after do

Wait Its no longer working it worked once
Its erroring again
18:54:15.811 ServerScriptService.Script:19: attempt to index nil with ‘UpperTorso’ - Server - Script:19
18:54:15.811 Stack Begin - Studio
18:54:15.811 Script ‘ServerScriptService.Script’, Line 19 - Studio - Script:19
18:54:15.812 Stack End

It stopped working because your character took more than 1 second to load in so the wait(1) was not sufficient.
Replace the wait(1) with two lines.

game.Workspace:WaitForChild(v.Name)
v.Character:WaitForChild("UpperTorso")

I’d also recommend switching to HumanoidRootPart instead of UpperTorso so your script is compatible with both R15 and R6 but that is up to you.

What do I do about this 19:08:33.019 Infinite yield possible on ‘Workspace.baseballcal15:WaitForChild(“HumanoidRootPart”)’ - Studio
19:08:33.019 Stack Begin - Studio
19:08:33.019 Script ‘ServerScriptService.Script’, Line 19 - Studio - Script:19
19:08:33.019 Stack End

Also why do I keep glitching through my maps
Just wanted some advice on this problem

To my knowledge the first issue should not be an error but just a warning. This should be fine but you can always add a timeout to make sure it doesn’t hold up the entire script forever, just add a comma followed by how long you want to wait. (You can read about it here Instance | Documentation - Roblox Creator Hub)

As for the glitching, I have no clue. Is it just happening in studio or also in the actual Roblox player? You can check if your fps is running at 60 by doing ctrl+shift+f5.