Attempt to index nil with 'PlayerGui'

Workspace.ErrorSans.ScreenString:39: attempt to index nil with ‘PlayerGui’

I’m reaching this error for reason’s why I dont understand?

This is the script :

if stringscreen == 1 then
	local one = script.String1:Clone()
	one.Parent = player.PlayerGui
	wait(7.5)
	one:Destroy()
	script:Destroy()
end

The error happens on line 3 but I don’t know why it doesnt work?
Help would be appreciated thank you.

1 Like

Hi kuu, it looks like Roblox Studio doesn’t recognize what you’re trying to reference, which is why it is saying player is “nil.”

Make sure to load the local player into your script before referencing it. Like msix29 pointed out, the LocalPlayer variable can also only be used in LocalScripts.

local player = game:GetService("Players").LocalPlayer
1 Like

what is the player defined as? If its game.Players.LocalPlayer it can NOT be used in a “normal” script

4 Likes

This is the entire script.


repeat wait() until script.Parent
local targetfound = false
repeat wait() until script.Parent.Name ~= "ErrorSansAttacks"
local mag = 4000
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

--================TargetPosition========================
	local players = game.Players:GetChildren()
		local range = 300
		local lastmag= 1000
		local mTorso = script.Parent.Parent:FindFirstChild("Torso")
		local mHum = script.Parent:FindFirstChild("Humanoid")
		local targetplayer = nil--players[1]
		for i=1,#players do
			wait()
		if players[i].Character then
			local char = players[i].Character
			if char:FindFirstChild("Torso") then
				local Torso = char:FindFirstChild("Torso")
				local mag = (Torso.Position - script.Parent.Torso.Position).magnitude
				if mag<=range then
					if char.Name ~= script.Parent.Parent then
					if mag < lastmag then
						mag = (Torso.Position - script.Parent.Torso.Position).magnitude
						lastmag = mag
						targetplayer = players[i]
					end
					end
				end
			end
			end
				end -- end of for loop
		

local stringscreen = math.random(1,3)
if stringscreen == 1 then
	local one = script.String1:Clone()
	one.Parent = player.PlayerGui
	wait(7.5)
	one:Destroy()
	script:Destroy()
end
if stringscreen == 2 then
	local two = script.String2:Clone()
	two.Parent = player.PlayerGui
	wait(7.5)
	two:Destroy()
	script:Destroy()
end
if stringscreen == 3 then
	local three = script.String3:Clone()
	three.Parent = player.PlayerGui
	wait(7.5)
	three:Destroy()
	script:Destroy()
end


				
				
			
	

--==========================

local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

I still dont know why it is not working but it shouldnt

P.S this is a serverscript not local

Where is the script placed?

Chaaaaarrrrrssssss

1 Like

Try replacing :GetPlayerFromCharacter(script.Parent.Parent) with .LocalPlayer

1 Like

It’s a serverscript not a localscript

1 Like

In an enemy, its a serverscript which gets the nearest player.

1 Like

You can’t access playerGui from a serverscript, so you should probably use a remoteEvent

1 Like

And how would I manage to do something like that?

1 Like

Let’s say that our remoteEvent (In replicated storage) is called “RemoteEvent”
In the serverscript, in place of the code in the main post, you could have:

if stringscreen == 1 then
    game.ReplicatedStorage:FireClient()
end

And in a localsript:

player = game.Players.LocalPlayer

game.ReplicatedStorage.OnClientEvent:Connect(function()
    local one = script.String1:Clone()
    one.Parent = player.PlayerGui
    wait(7.5)
    one:Destroy()
    script:Destroy()
end)
2 Likes

Like this?
Server Script :


repeat wait() until script.Parent
local targetfound = false
repeat wait() until script.Parent.Name ~= "ErrorSansAttacks"
local mag = 4000
local player = game.Players:GetPlayerFromCharacter(script.Parent.Parent)

--================TargetPosition========================
	local players = game.Players:GetChildren()
		local range = 300
		local lastmag= 1000
		local mTorso = script.Parent.Parent:FindFirstChild("Torso")
		local mHum = script.Parent:FindFirstChild("Humanoid")
		local targetplayer = nil--players[1]
		for i=1,#players do
			wait()
		if players[i].Character then
			local char = players[i].Character
			if char:FindFirstChild("Torso") then
				local Torso = char:FindFirstChild("Torso")
				local mag = (Torso.Position - script.Parent.Torso.Position).magnitude
				if mag<=range then
					if char.Name ~= script.Parent.Parent then
					if mag < lastmag then
						mag = (Torso.Position - script.Parent.Torso.Position).magnitude
						lastmag = mag
						targetplayer = players[i]
					end
					end
				end
			end
			end
				end -- end of for loop
		

local stringscreen = math.random(1,3)
if stringscreen == 1 then
	game.ReplicatedStorage.GUIEvent:FireClient(player)
end
if stringscreen == 2 then
	game.ReplicatedStorage.GUIEvent:FireClient(player)
end
if stringscreen == 3 then
	game.ReplicatedStorage.GUIEvent:FireClient(player)
end


				
				
			
	

--==========================

Local Script :

player = game.Players.LocalPlayer

game.ReplicatedStorage.GUIEvent.OnClientEvent:Connect(function(one)
	local one = script.Parent.String1:Clone()
	one.Parent = player.PlayerGui
	wait(7.5)
	one:Destroy()
	script:Destroy()
end)

game.ReplicatedStorage.GUIEvent.OnClientEvent:Connect(function(two)
	local two = script.Parent.String2:Clone()
	two.Parent = player.PlayerGui
	wait(7.5)
	two:Destroy()
	script:Destroy()
end)

game.ReplicatedStorage.GUIEvent.OnClientEvent:Connect(function(three)
	local three = script.Parent.String3:Clone()
	three.Parent = player.PlayerGui
	wait(7.5)
	three:Destroy()
	script:Destroy()
end)

image

Yeah, I think that should work.
One thing, do you want one / two / three to be cloned for everyone?
If so, replace FireClient with FireAllClients

1 Like

Nopeeee

Didnt work with just player either
image

Where is the script located?

char limit

1 Like

Oh, right.
Use game.Players.LocalPlayer instead of player in the localscript.

1 Like

It is located in a model in a boss, firstly its in serverstorage but it is cloned to the bosses model

1 Like

Then you can’t get the player from a model of the boss?

script.Parent.Parent would be equal to the boss character?

Try this script I made:

repeat wait() until script.Parent
local targetfound = false
repeat wait() until script.Parent.Name ~= "ErrorSansAttacks"
local mag = 4000

game.Players.PlayerAdded:Connect(function(player)
--================TargetPosition========================
	local players = game.Players:GetChildren()
		local range = 300
		local lastmag= 1000
		local mTorso = script.Parent.Parent:FindFirstChild("Torso")
		local mHum = script.Parent:FindFirstChild("Humanoid")
		local targetplayer = nil--players[1]
		for i=1,#players do
			wait()
		if players[i].Character then
			local char = players[i].Character
			if char:FindFirstChild("Torso") then
				local Torso = char:FindFirstChild("Torso")
				local mag = (Torso.Position - script.Parent.Torso.Position).magnitude
				if mag<=range then
					if char.Name ~= script.Parent.Parent then
					if mag < lastmag then
						mag = (Torso.Position - script.Parent.Torso.Position).magnitude
						lastmag = mag
						targetplayer = players[i]
					end
					end
				end
			end
			end
				end -- end of for loop
		

local stringscreen = math.random(1,3)
if stringscreen == 1 then
	local one = script.String1:Clone()
	one.Parent = player.PlayerGui
	wait(7.5)
	one:Destroy()
	script:Destroy()
end
if stringscreen == 2 then
	local two = script.String2:Clone()
	two.Parent = player.PlayerGui
	wait(7.5)
	two:Destroy()
	script:Destroy()
end
if stringscreen == 3 then
	local three = script.String3:Clone()
	three.Parent = player.PlayerGui
	wait(7.5)
	three:Destroy()
	script:Destroy()
end

end)
				
				
			
	

--==========================

Check if the player is nil by using print(player) where it is defined

Also, always use WaitForChild when using a PlayerGui because oftentimes it doesn’t load in immediately when the game is run

Hi @Kurtziru , you’re original should not have problem.

My problem in my opinion lies here:

You’re destroy the GUI with a script that has already been deleted.
So, Just after:

one:Destroy() -- // Add
task.wait(0.5)
script:Destroy()

Although in the script is in order this operation takes place in very few milliseconds and could lead to that error