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

I just started testing my scripts in game... errors ?

Asked by
Bulvyte 388 Moderation Voter
7 years ago
Edited 7 years ago

ALL OF them work in F6 or solo mode but not in game mode. i know cuz scripts load faster than player, but how u fix it ?

I get this error: attempt to index field 'LocalPlayer' (a nil value) and i have no IDEA how to fix this

This script is confusing me alot, also this isn't a localscript it doesn't even work in a localscritp cuz its in a brick.

function teleportPlayer(pos, torso)
local char = torso
char.CFrame = CFrame.new(Vector3.new(pos.x, pos.y, pos.z))
end

lvl = script.Parent.Parent.Parent.LevelValue

Door = script.Parent
script.Parent.Touched:connect(function(part)
    print("Door Hit")
    local human = part.Parent:FindFirstChild("Humanoid")
    if (human ~= nil) then
        print("Human touched door")
        local player = game.Players:GetPlayerFromCharacter(human.Parent) 
        if (player == nil) then return end 
        local stats = player:FindFirstChild("leaderstats") 
        local sp = stats:FindFirstChild("test") 
        if sp == nil then return false end 
        print("Human passed test")
    else
        print("nope.avi")

        GUI = Instance.new("ScreenGui")--                             problem here <<<<<
        GUI.Name = "ScreenGuiLVL"
        GUI.Parent = game.Players.LocalPlayer.PlayerGui

Also, why does even the error happen about player nil if this is in a brick ? it should get the player by touching ?

this script works everywhere else but not in-game.

1
I am pretty sure nathan answered your question. TheLowOne 85 — 7y
3
The reason it works in Solo mode, is because there is only one player in solo mode, so LocalPlayer becomes the player and is accessible by every script in the game. In a real server, you can't use LocalPlayer because there are multiple players. darkelementallord 686 — 7y

2 answers

Log in to vote
5
Answered by
Async_io 908 Moderation Voter
7 years ago
Edited 7 years ago
function teleportPlayer(pos, torso)
local char = torso
char.CFrame = CFrame.new(Vector3.new(pos.x, pos.y, pos.z))
end

lvl = script.Parent.Parent.Parent.LevelValue
Door = script.Parent

script.Parent.Touched:connect(function(part)
    print("Door Hit")
    local human = part.Parent:FindFirstChild("Humanoid")
    if human ~= nil then
        print("Human touched door")
        local player = game.Players:GetPlayerFromCharacter(human.Parent) 
        if (player == nil) then return end 
        local stats = player:FindFirstChild("leaderstats") 
        local sp = stats:FindFirstChild("test") 
        if sp == nil then return false end 
        print("Human passed test")
    else
        print("nope.avi")

        GUI = Instance.new("ScreenGui") --creates a screen gui
        GUI.Parent = player.PlayerGui --Problem fixed. (Parents it)
        GUI.Name = "ScreenGuiLVL" --Gives it a name

LocalPlayer is not usable in a Normal Script. Click for more information.

0
was what I said wrong? TheLowOne 85 — 7y
0
Please read what i wrote i dont like ppl who skips explaining and go instantly to answering i know i nees the frame to see but instancing the acreengui doesnt work it gives me an error bro pls finish reading everything before answering ... Bulvyte 388 — 7y
0
I did read everything. I don't understand what you mean. Async_io 908 — 7y
0
If it gives you an error, you have to tell me what the error is. Async_io 908 — 7y
View all comments (5 more)
1
Player was not redefined... Also Bulvtye, this script detects if a player touche it, then does the stats stuff. If something other than a player touches it, it makes the screengui, but where do you want to put the screengui? darkelementallord 686 — 7y
0
I wrote exactly attempt to index localplayer a nil value ot something like that read up Bulvyte 388 — 7y
0
I want the acreengui to be put in playergui Bulvyte 388 — 7y
0
Ok so defining player already gets it to the player no need for localplayer ? Then what does localplayer do may i ask ? ;) Bulvyte 388 — 7y
0
LocalPlayer can ONLY BE USED IN LOCAL SCRIPT. This is not a local script. LocalPlayer grabs the player from a local script. You already had the player, so it's pointless to use LocalPlayer since you can't even use it in a normal script, which is what you should use. Async_io 908 — 7y
Ad
Log in to vote
2
Answered by 7 years ago
Edited 7 years ago

The only way to access LocalPlayer from Players instance, is through localscript

Edit (6:22) - My bad nathan, didnt mean to give wrong idea, I removed it from answer Localscript LocalPlayer

1
-1? TheLowOne 85 — 7y
0
It's impossible to use a local script in a workspace. Async_io 908 — 7y
1
I was only referencing the fact that he had LocalPlayer being used in his script, but thanks for the feedback!! TheLowOne 85 — 7y
0
'If this is happening, make sure it is a local script' Async_io 908 — 7y
View all comments (3 more)
1
I re-edited, thanks TheLowOne 85 — 7y
1
Technically, a local script can be ran inside of workspace if it's inside a character :3 User#11440 120 — 7y
1
But then it's not a direct child of Workspace, now, is it? Link150 1355 — 7y

Answer this question