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 field 'LocalPlayer' (a nil value)?

Hey, so I've been trying to teleport everyone in the server to a specific block, however, I keep getting the error of: "Attempt to Index local 'player' (A nil value.) Any help?

 function startGame()
	local AvaliableMaps = MapsFolder:GetChildren()
	local SelectedMap = AvaliableMaps[math.random(1, #AvaliableMaps)]
	SelectedMap:Clone()
	SelectedMap.Parent = workspace
	ForText.Value = "Test"
	local RealEditingMap = SelectedMap
	print(RealEditingMap)
	local EveryoneElseBlock = RealEditingMap.Jail.EveryoneElse
	for _, Player in next, game:GetService("Players"):GetChildren() do
	local Torso = Player.Character:FindFirstChild("Torso")
		if Torso then
		Torso.CFrame = CFrame.new(EveryoneElseBlock.Position)
	end
end
end

Then Comes up with this error: Link

6 comments

0

is this a regular script in the server or a local script on the client?

0

This is a regular script

0

so it's in the server?

1

you can't get 'LocalPlayer' from the server. That can only be accessed from the client in a localscript

0

However, is in a bindable function which is all sorted, as message with a print (print("hello world")). I only added the part where it comes up with the message.

0

Oh, then what should I do?

2 answers

✓ Accepted answer

You cannot access LocalPlayer from this server, so here is a fix.

local EveryoneElseBlock = game.Workspace.EveryoneElseBlock
for _, Player in next, game:GetService("Players"):GetChildren() do
    local Torso = Player.Character:FindFirstChild("Torso") or Player.Character:FindFirstChild("UpperTorso")
    if Torso then
	    Torso.CFrame = CFrame.new(EveryoneElseBlock.Position) + Vector3.new(0,5,0)
    end
end

this should work

7 comments

0

Well there's no error message, however it doesn't teleport me to the map

0

try changing 'Torso.CFrame = CFrame.new(EveryoneElseBlock.Position) + Vector3.new(0,5,0)' to 'Torso.CFrame = EveryoneElseBlock.CFrame'

0

ok

0

It didn't work. I tried to change the 'If Torso then'. I added a print before teleportation saying print('True'), but I also added an else statement with the line print('False') Output comes up with False. Therefore, it has to be a problem with line 3, local Torso = Player.Character:FindFirstChild("Torso").

0

ok how about this, change local Torso = Player.Character:FindFirstChild("Torso") to local Torso = Player.Character:FindFirstChild("Torso") or Player.Character:FindFirstChild("UpperTorso")

0

ok

0

Beaufiul! Thanks so much!

You should know what this is I just changed the loop to get players.

 function startGame()
	local AvaliableMaps = MapsFolder:GetChildren()
	local SelectedMap = AvaliableMaps[math.random(1, #AvaliableMaps)]
	SelectedMap:Clone()
	SelectedMap.Parent = workspace
	ForText.Value = "Test"
	local RealEditingMap = SelectedMap
	print(RealEditingMap)
	local EveryoneElseBlock = RealEditingMap.Jail.EveryoneElse
	for _,v in pairs(game:GetService("Players"):GetPlayers()) do
	local Torso = Player.Character:FindFirstChild("Torso")
		if Torso then
		Torso.CFrame = CFrame.new(EveryoneElseBlock.Position)
	end
end
end

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