What do you want to achieve? Keep it simple and clear!
I want to have a GUI appear.
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!
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
Stack Begin - Studio
Script ‘ServerScriptStorage.DeathLogic’, Line 56 - Studio - DeathLogic:56
Stack End - Studio
ServerScriptStorage.DeathLogic:56: attempt to index nil with ‘PlayerGui’
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)
-- 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.
-- 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)
-- 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)