Hello! I am trying to get the variable “D5” which is a textlabel but I keep getting this error: Workspace.Scout Cabins.CampingCabin.EntireDoor.Door.DoorScript:10: attempt to index nil with 'WaitForChild'
Here is my script:
local door = script.Parent
local doorKnob = door:FindFirstChild("DoorKnobFront")
local clickDetector = doorKnob:FindFirstChild("ClickDetector")
local DoorLocked = door.Sounds.DoorLocked
local DoorUnlocked = door.Sounds.DoorUnlocked
local Hinge = door:FindFirstChild("Hinge")
local Player = game:GetService("Players").LocalPlayer
local D5 = Player:WaitForChild("PlayerGui"):WaitForChild("Self-Dialogue"):WaitForChild("ScreenGui").D5
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local Goals = { TextTransparency = 1 }
local lockedResponses = {
"You: It's locked.",
"You: I can't open it.",
"You: The door won't budge.",
"You: Why am I doing this?",
"You: I think I need a key.",
}
local isLocked = true
local function interact(player)
if isLocked then
-- Check if the player has a key equipped
local keyEquipped = false
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool.Name == "CabinKey" then
keyEquipped = true
break
end
end
if keyEquipped then
isLocked = false
DoorUnlocked:Play()
else
local randomResponse = lockedResponses[math.random(1, #lockedResponses)]
D5.Text = randomResponse
local TweenDialogue = TweenService:Create(D5, Info, Goals)
D5.TextTransparency = 0
wait(4)
TweenDialogue:Play()
DoorLocked:Play()
end
else
for i = 1, 20 do
door:SetPrimaryPartCFrame(Hinge.CFrame * CFrame.Angles(0, math.rad(5), 0))
wait()
end
end
end
clickDetector.MouseClick:Connect(function(player)
interact(player)
end)
Why are you trying to change the Text on the server? Because gui is client oriented, Consider using Remote Events to handle Text changing on Screen Gui objects on the client.
Also btw:
:SetPrimaryPartCFrame() is deprecated and you should use PivotTo() instead
Oh and. LocalPlayer can only be used by local scripts. Using this in a server script, LocalPlayer will return nil. Use another method to get the player
Hey, I followed your suggestion, would this work? I did use quite a few functions since it’s easier for me to format
Server Script:
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local DoorInteractEvent = Instance.new("RemoteEvent")
DoorInteractEvent.Name = "DoorInteractEvent"
DoorInteractEvent.Parent = ReplicatedStorage
local function interactHandler(player)
local hasKey = false
for _, tool in pairs(player.Backpack:GetChildren()) do
if tool:IsA("Tool") and tool.Name == "CabinKey" then
hasKey = true
break
end
end
if hasKey then
DoorInteractEvent:FireClient(player, "Unlock")
else
DoorInteractEvent:FireClient(player, "KeyRequired")
end
end
DoorInteractEvent.OnServerEvent:Connect(interactHandler)
Local Script:
local door = script.Parent
local doorKnob = door:FindFirstChild("DoorKnobFront")
local clickDetector = doorKnob:FindFirstChild("ClickDetector")
local DoorLocked = door.Sounds.DoorLocked
local DoorUnlocked = door.Sounds.DoorUnlocked
local Hinge = door:FindFirstChild("Hinge")
local Player = game:GetService("Players").LocalPlayer
local UserInputService = game:GetService("UserInputService")
local DoorInteractEvent = game:GetService("ReplicatedStorage"):WaitForChild("DoorInteractEvent")
local D5 = Player:WaitForChild("PlayerGui"):WaitForChild("Self-Dialogue"):WaitForChild("ScreenGui"):WaitForChild("D5")
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local Goals = { TextTransparency = 1 }
local lockedResponses = {
"You: It's locked.",
"You: I can't open it.",
"You: The door won't budge.",
"You: Why am I doing this?",
"You: I think I need a key.",
}
local function setDialogueText(text)
D5.Text = text
end
local function fadeInDialogue()
D5.TextTransparency = 0
local TweenDialogue = TweenService:Create(D5, Info, Goals)
wait(4)
TweenDialogue:Play()
end
local function playLockedSound()
DoorLocked:Play()
end
local function openDoor()
for i = 1, 20 do
door:PivotTo(Hinge.CFrame * CFrame.Angles(0, math.rad(5), 0))
wait()
end
end
local function handleDoorInteraction()
DoorInteractEvent:FireServer()
end
clickDetector.MouseClick:Connect(function()
handleDoorInteraction()
end)
local function handleLockedDoorResponse()
local randomResponse = lockedResponses[math.random(1, #lockedResponses)]
setDialogueText(randomResponse)
fadeInDialogue()
playLockedSound()
end
DoorInteractEvent.OnClientEvent:Connect(function(response)
if response == "Unlock" then
DoorUnlocked:Play()
openDoor()
elseif response == "KeyRequired" then
handleLockedDoorResponse()
end
end)
Hi, It’s fine. I had other things to do. But, I am experiencing an issue though. Everything in both scripts give me no errors but when I test out the game, it isn’t working. Here’s the footage:
Can you try adding some prints, and see whats running. Add some prints in each function (& in both scripts), and see if it runs through all lines of code
Where is the local script, server script, remote event located (their parent)?
I don’t think you have the local script in a placee where local scripts run. It should be located in Starterplayer < StarterPlayerScripts, and you should change this in the local script:
I moved the local script to StarterPlayerScripts since it was originally in the door model. It seems to work a bit now that I am receiving one error. Which is this:
local door = game.Workspace.ScoutCabins.CampingCabin2.EntireDoor:WaitForChild("Door")
local doorKnob = door.DoorKnobFront
local clickDetector = doorKnob.ClickDetector
local DoorLocked = door.Sounds.DoorLocked
local DoorUnlocked = door.Sounds.DoorUnlocked
local Hinge = door.Hinge
local Player = game:GetService("Players").LocalPlayer
local DoorInteractEvent = game:GetService("ReplicatedStorage"):WaitForChild("DoorInteractEvent")
local D5 = Player.PlayerGui["Self-Dialogue"]:WaitForChild("ScreenGui").D5
local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(1, Enum.EasingStyle.Sine, Enum.EasingDirection.In, 0, false, 0)
local Goals = { TextTransparency = 1 }
local lockedResponses = {
"You: It's locked.",
"You: I can't open it.",
"You: The door won't budge.",
"You: Why am I doing this?",
"You: I think I need a key.",
}
local function setDialogueText(text)
print("Setting the text")
D5.Text = text
end
local function fadeInDialogue()
print("TweeningText")
D5.TextTransparency = 0
local TweenDialogue = TweenService:Create(D5, Info, Goals)
wait(4)
TweenDialogue:Play()
end
local function playLockedSound()
print("Playing Locked Sound")
DoorLocked:Play()
end
local function openDoor()
print("Opening door")
for i = 1, 20 do
door:PivotTo(Hinge.CFrame * CFrame.Angles(0, math.rad(5), 0))
wait()
end
end
local function handleDoorInteraction()
print("Firing door event")
DoorInteractEvent:FireServer()
end
clickDetector.MouseClick:Connect(function()
print("Player clicked mouse")
handleDoorInteraction()
end)
local function handleLockedDoorResponse()
print("Handling random responses")
local randomResponse = lockedResponses[math.random(1, #lockedResponses)]
setDialogueText(randomResponse)
fadeInDialogue()
playLockedSound()
end
DoorInteractEvent.OnClientEvent:Connect(function(response)
if response == "Unlock" then
DoorUnlocked:Play()
openDoor()
elseif response == "KeyRequired" then
handleLockedDoorResponse()
end
end)
Everything seems to work as intended. Although there are a few bugs with it but I think I can fix them on my own. It wouldn’t be a great idea to explain it here unless I make another topic. Other than that, everything works now. Thanks!