Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

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

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.

2 comments

1

I am pretty sure nathan answered your question.

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.

2 answers

✓ Accepted answer
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.

9 comments

0

was what I said wrong?

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 ...

0

I did read everything. I don't understand what you mean.

0

If it gives you an error, you have to tell me what the error is.

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?

0

I wrote exactly attempt to index localplayer a nil value ot something like that read up

0

I want the acreengui to be put in playergui

0

Ok so defining player already gets it to the player no need for localplayer ? Then what does localplayer do may i ask ? ;)

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.

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

7 comments

1

-1?

0

It's impossible to use a local script in a workspace.

1

I was only referencing the fact that he had LocalPlayer being used in his script, but thanks for the feedback!!

0

'If this is happening, make sure it is a local script'

1

I re-edited, thanks

1

Technically, a local script can be ran inside of workspace if it's inside a character :3

1

But then it's not a direct child of Workspace, now, is it?

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