Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Attempt to index field 'LocalPlayer' (a nil value)?

Asked by 5 years ago
Edited 5 years ago

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

0
is this a regular script in the server or a local script on the client? alex_ander 163 — 5y
0
This is a regular script LennyPlayzYT 269 — 5y
0
so it's in the server? alex_ander 163 — 5y
1
you can't get 'LocalPlayer' from the server. That can only be accessed from the client in a localscript alex_ander 163 — 5y
View all comments (2 more)
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. LennyPlayzYT 269 — 5y
0
Oh, then what should I do? LennyPlayzYT 269 — 5y

2 answers

Log in to vote
0
Answered by 5 years ago
Edited 5 years ago

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

0
Well there's no error message, however it doesn't teleport me to the map LennyPlayzYT 269 — 5y
0
try changing 'Torso.CFrame = CFrame.new(EveryoneElseBlock.Position) + Vector3.new(0,5,0)' to 'Torso.CFrame = EveryoneElseBlock.CFrame' alex_ander 163 — 5y
0
ok LennyPlayzYT 269 — 5y
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"). LennyPlayzYT 269 — 5y
View all comments (3 more)
0
ok how about this, change `local Torso = Player.Character:FindFirstChild("Torso")` to `local Torso = Player.Character:FindFirstChild("Torso") or Player.Character:FindFirstChild("UpperTorso")` alex_ander 163 — 5y
0
ok LennyPlayzYT 269 — 5y
0
Beaufiul! Thanks so much! LennyPlayzYT 269 — 5y
Ad
Log in to vote
0
Answered by 5 years ago

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

Answer this question