Nil with PLayerGUI but it still works

  1. What do you want to achieve? Keep it simple and clear!
    I want to have a GUI appear.

  2. What is the issue? Include screenshots / videos if possible!
    Nothing! There is no error. The script looks great. But, there’s still an error after I got the GUI from the PlayerGui, and it says “attempt to index nil with ‘PlayerGui’”, but it still works

I would like to wonder why this error is coming up if it’s getting the GUI just fine, and how to fix it because it’s cloggin up my output.

For more info:

-- Place this script in the part that triggers the teleport
local modelToTeleport = workspace.Jumpscare
local mainmonst = workspace.MainMonst
local teleportPart = mainmonst.HumanoidRootPart
local teleportDistance = 3
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraControlEvent = ReplicatedStorage:WaitForChild("CameraControlEvent")


-- Animation ID (Replace with your animation's ID)
local animationId = "rbxassetid://15737482222"

local function teleportModel(player, gui)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		-- Stop the player's movement
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0

		-- Load and play the animation
		local animation = Instance.new("Animation")
		animation.AnimationId = animationId
		local animationTrack = modelToTeleport.AnimationController:LoadAnimation(animation)
		animationTrack:Play()
		
		-- Lock the camera
		cameraControlEvent:FireClient(player, true)

		-- Teleportation logic here (same as your existing code)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local playerDirection = humanoidRootPart.CFrame.lookVector
		local teleportPosition = humanoidRootPart.Position + (playerDirection * teleportDistance)
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition))
		local modelFacingDirection = (humanoidRootPart.Position - modelToTeleport.PrimaryPart.Position).unit
		mainmonst:SetPrimaryPartCFrame(CFrame.new(workspace.tpmonst.Position))
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition, teleportPosition + modelFacingDirection))
		
		local headPosition = modelToTeleport:FindFirstChild("Head").Position
		cameraControlEvent:FireClient(player, true, headPosition) -- Lock the camera and send head position

		-- Wait for the duration of the teleportation/animation
		wait(1) -- Adjust the duration as needed
		print("wait done")
		print(gui)
		gui.Enabled = true
		gui.TextLabel.Visible = false
		wait(3)
		gui.TextLabel.Visible = true
		
		
	end
end

teleportPart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local death = player.PlayerGui.death
	if player then
		teleportModel(player, death)
	end
end)

This script is a jumpscare script- and what it does is teleport the MainMonster (the one that chases the player) to a box somewhere else and teleport another in front of the player, and makes them look at the head of the monster. Which I then want it to enable the “Death” GUI, and then make the text visible. Thanks for the help!

Do you ever define PlayerGui (“gui”) as a variable?

It’s in the function, the variable that does INTO the function is called death here:

teleportPart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local death = player.PlayerGui.death
	if player then
		teleportModel(player, death)
	end
end)

Sorry if I misunderstood by the way, I’m a beginner haha

Doing ctrl+f, I find that before this block of code,
image

you never define gui, it’s nil.

never mind its in the fucntion i missee

Can you show the death GUI and its descendants? (Screenshot)

image

This is all the ScreenGuis btw

what;s the script in the textalbel .?

local text = script.Parent

while true do
	local r = math.random(0, 100)
	wait(0.1)
	text.TextTransparency = (r/100)
end

It’s a flicker script that makes the text look almost like a candle flame, it’s just visual

What line does your main script error?

Line 56, here’s the full error:

Stack Begin - Studio
Script ‘ServerScriptStorage.DeathLogic’, Line 56 - Studio - DeathLogic:56
Stack End - Studio
ServerScriptStorage.DeathLogic:56: attempt to index nil with ‘PlayerGui’

here’s a screenshot of ServerScriptStorage:

But, line 56 is teleportpart.touched. Are you leaving out any code?

No, the script works completely fine it’s just the error that’s confusing me, if it’s Nil then it shouldn’t be working- but it is working so i’m just wondering why and how to fix because it’s super annoying. The onlt other script is the local script that makes it so the players camera looks at the monster’s head.

Here (LocalScript in StartPlayerScripts):

-- LocalScript
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraControlEvent = ReplicatedStorage:WaitForChild("CameraControlEvent")
local player = game.Players.LocalPlayer
local camera = game.Workspace.CurrentCamera

local function lockCamera(lock, headPosition)
	if lock then
		camera.CameraType = Enum.CameraType.Scriptable
		if headPosition then
			camera.CFrame = CFrame.new(camera.CFrame.Position, headPosition)
		end
	else
		camera.CameraType = Enum.CameraType.Custom
	end
end

cameraControlEvent.OnClientEvent:Connect(lockCamera)

try this code

-- Place this script in the part that triggers the teleport
local modelToTeleport = workspace.Jumpscare
local mainmonst = workspace.MainMonst
local teleportPart = mainmonst.HumanoidRootPart
local teleportDistance = 3
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraControlEvent = ReplicatedStorage:WaitForChild("CameraControlEvent")


-- Animation ID (Replace with your animation's ID)
local animationId = "rbxassetid://15737482222"

local function teleportModel(player, gui)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		-- Stop the player's movement
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0

		-- Load and play the animation
		local animation = Instance.new("Animation")
		animation.AnimationId = animationId
		local animationTrack = modelToTeleport.AnimationController:LoadAnimation(animation)
		animationTrack:Play()

		-- Lock the camera
		cameraControlEvent:FireClient(player, true)

		-- Teleportation logic here (same as your existing code)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local playerDirection = humanoidRootPart.CFrame.lookVector
		local teleportPosition = humanoidRootPart.Position + (playerDirection * teleportDistance)
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition))
		local modelFacingDirection = (humanoidRootPart.Position - modelToTeleport.PrimaryPart.Position).unit
		mainmonst:SetPrimaryPartCFrame(CFrame.new(workspace.tpmonst.Position))
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition, teleportPosition + modelFacingDirection))

		local headPosition = modelToTeleport:FindFirstChild("Head").Position
		cameraControlEvent:FireClient(player, true, headPosition) -- Lock the camera and send head position

		-- Wait for the duration of the teleportation/animation
		wait(1) -- Adjust the duration as needed
		print("wait done")
		print(gui)
		gui.Enabled = true
		gui.TextLabel.Visible = false
		wait(3)
		gui.TextLabel.Visible = true


	end
end

teleportPart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local death = player:WaitForChild("PlayerGui"):WaitForChild("death")
	if player then
		teleportModel(player, death)
	end
end)

haha so the same thing happened- an error came up (ServerScriptStorage.DeathLogic:58: attempt to index nil with ‘WaitForChild’) but it still worked COMPLETLEY fine.

that dose not make cents.

I know it’s so weird and I have no idea why it’s happening-

try this,

-- Place this script in the part that triggers the teleport
local modelToTeleport = workspace.Jumpscare
local mainmonst = workspace.MainMonst
local teleportPart = mainmonst.HumanoidRootPart
local teleportDistance = 3
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraControlEvent = ReplicatedStorage:WaitForChild("CameraControlEvent")


-- Animation ID (Replace with your animation's ID)
local animationId = "rbxassetid://15737482222"

local function teleportModel(player, gui)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		-- Stop the player's movement
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0

		-- Load and play the animation
		local animation = Instance.new("Animation")
		animation.AnimationId = animationId
		local animationTrack = modelToTeleport.AnimationController:LoadAnimation(animation)
		animationTrack:Play()

		-- Lock the camera
		cameraControlEvent:FireClient(player, true)

		-- Teleportation logic here (same as your existing code)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local playerDirection = humanoidRootPart.CFrame.lookVector
		local teleportPosition = humanoidRootPart.Position + (playerDirection * teleportDistance)
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition))
		local modelFacingDirection = (humanoidRootPart.Position - modelToTeleport.PrimaryPart.Position).unit
		mainmonst:SetPrimaryPartCFrame(CFrame.new(workspace.tpmonst.Position))
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition, teleportPosition + modelFacingDirection))

		local headPosition = modelToTeleport:FindFirstChild("Head").Position
		cameraControlEvent:FireClient(player, true, headPosition) -- Lock the camera and send head position

		-- Wait for the duration of the teleportation/animation
		wait(1) -- Adjust the duration as needed
		print("wait done")
		print(gui)
		gui.Enabled = true
		gui.TextLabel.Visible = false
		wait(3)
		gui.TextLabel.Visible = true


	end
end

teleportPart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	local death = player.PlayerGui:WaitForChild("death")
	if player then
		teleportModel(player, death)
	end
end)

same thing. same error as the start but the GUI is appearing the same. error in case you need it:

ServerScriptStorage.DeathLogic:58: attempt to index nil with ‘PlayerGui’

this

-- Place this script in the part that triggers the teleport
local modelToTeleport = workspace.Jumpscare
local mainmonst = workspace.MainMonst
local teleportPart = mainmonst.HumanoidRootPart
local teleportDistance = 3
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local cameraControlEvent = ReplicatedStorage:WaitForChild("CameraControlEvent")


-- Animation ID (Replace with your animation's ID)
local animationId = "rbxassetid://15737482222"

local function teleportModel(player, gui)
	local character = player.Character or player.CharacterAdded:Wait()
	local humanoid = character:FindFirstChild("Humanoid")

	if humanoid then
		-- Stop the player's movement
		humanoid.WalkSpeed = 0
		humanoid.JumpPower = 0

		-- Load and play the animation
		local animation = Instance.new("Animation")
		animation.AnimationId = animationId
		local animationTrack = modelToTeleport.AnimationController:LoadAnimation(animation)
		animationTrack:Play()

		-- Lock the camera
		cameraControlEvent:FireClient(player, true)

		-- Teleportation logic here (same as your existing code)
		local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
		local playerDirection = humanoidRootPart.CFrame.lookVector
		local teleportPosition = humanoidRootPart.Position + (playerDirection * teleportDistance)
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition))
		local modelFacingDirection = (humanoidRootPart.Position - modelToTeleport.PrimaryPart.Position).unit
		mainmonst:SetPrimaryPartCFrame(CFrame.new(workspace.tpmonst.Position))
		modelToTeleport:SetPrimaryPartCFrame(CFrame.new(teleportPosition, teleportPosition + modelFacingDirection))

		local headPosition = modelToTeleport:FindFirstChild("Head").Position
		cameraControlEvent:FireClient(player, true, headPosition) -- Lock the camera and send head position

		-- Wait for the duration of the teleportation/animation
		wait(1) -- Adjust the duration as needed
		print("wait done")
		print(gui)
		gui.Enabled = true
		gui.TextLabel.Visible = false
		wait(3)
		gui.TextLabel.Visible = true


	end
end

teleportPart.Touched:Connect(function(hit)
	local player = game.Players:GetPlayerFromCharacter(hit.Parent)
	pcall(function()
		local death = player.PlayerGui:WaitForChild("death")
		if player then
			teleportModel(player, death)
		end
	end)
end)

try