I am a Data Technician with specialization in programming and program games in my free time. I’m going inside, trying to code this for over 10 hours. I have a simple door and its script has an open and close door state. It uses ProximityPrompt.
Goal
This door that I am scripting now needs an isLocked state. When the door isLocked, it will check if the player has an item. A ScreenGUI will appear and its text will change depending on the door’s state (fx. close / open = "Press E to close or for isLocked when true locked = "This is locked. Find something to open it with. It has to use the KeyCode “E” for the interaction.
Issue
I have so many versions and might just be too tired, but I have tried for over 10 hours this task. I cannot comprehend how a ScreenGUI will be manipulated from a script. It didn’t exactly let me manipulate it unless it was within StarterGUI -> ScreenGUI -> TextLabel -> LocalScript.
Concern
Am I missing something or am I just not thinking clearly enough to see the easy path?
•▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬▬•
Let me know if I have missed something and feel free to link any external links in assistance to solving the issue.
It would help if you copy/pasted the section of code here with 3 backticks (```) before and after so it formats properly.
This would help us see how you’re checking the player’s inventory as well as how the code is affecting the GUI.
I always print variables before if statements to see if there are any weird values that can be traced back to somewhere else. For example if I have a line like if locked == true then I’ll put print(locked) before it to see if it’s coming up as nil or something else which would mean I’d have to look at the section of code that sets the locked value.
-- Initializes services, sounds, variables
local tweenService = game:GetService("TweenService")
local doorToggleRequest = game:GetService("ReplicatedStorage"):WaitForChild("DoorToggleRequest")
local updateDoorStatus = game:GetService("ReplicatedStorage"):WaitForChild("UpdateDoorStatus")
local door = workspace.TraditionalDoor.Door
local hinge = workspace.TraditionalDoor.Hinge
local openSound = workspace.TraditionalDoor.Door.openSound
local closeSound = workspace.TraditionalDoor.Door.closeSound
-- Defines door states
local openCFrame = hinge.CFrame * CFrame.Angles(0, math.rad(90), 0)
local closeCFrame = hinge.CFrame
local tweenDuration = 1
local tweenInfo = TweenInfo.new(tweenDuration)
local tweenOpen = tweenService:Create(hinge, tweenInfo, { CFrame = openCFrame })
local tweenClose = tweenService:Create(hinge, tweenInfo, { CFrame = closeCFrame })
local isLocked = true
local closed = true
local requiredKey = "itemTest" -- Defines required key
-- Function checks if player has key
local function playerHasKey(player)
local backpack = player:FindFirstChild("Backpack")
if backpack then
for _, item in pairs(backpack:GetChildren()) do
if item.Name == requiredKey then
return true
end
end
end
return false
end
-- Function toggles door state and update UI
local function toggleDoor(player)
if isLocked and not playerHasKey(player) then
updateDoorStatus:FireClient(player, "This is locked. Find something to unlock it.")
else
if not isLocked and closed then
tweenOpenPlay()
openSound:Play()
closed = false
updateDoorStatus:FireClient(player, "Press E to close")
else
tweenClosePlay()
closeSound:Play()
closed = true
updateDoorStatus:FireClient(player, "Press E to open")
end
end
end
-- Connecst toggle function to RemoteEvent
doorToggleRequest.OnServerEvent:Connect(toggleDoor)
LocalScript(game.StarterGUI.keyScreenGUI.keyItem)
local player = game.Players.LocalPlayer
local userInputService = game:GetService("UserInputService")
local doorToggleRequest = game:GetService("ReplicatedStorage"):WaitForChild("DoorToggleRequest")
local updateDoorStatus = game:GetService("ReplicatedStorage"):WaitForChild("UpdateDoorStatus")
-- UI elements
local screenGui = player:WaitForChild("PlayerGui"):WaitForChild("keyScreenGui")
local textLabel = screenGui:WaitForChild("keyText")
-- Updates UI based on door status messages from server
updateDoorStatus.OnClientEvent:Connect(function(statusMessage)
textLabel.Text = statusMessage
end)
-- Send door toggle request if within range and "E" is pressed
userInputService.InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.E then
local character = player.Character or player.CharacterAdded:Wait()
local door = workspace:WaitForChild("LockedTraditional").Door
local distance = (character.PrimaryPart.Position - door.Position).Magnitude
if distance <= 10 then -- Adjusts distance
doorToggleRequest:FireServer() -- Sends toggle request to server
end
end
end)
local function toggleDoor(player)
print("isLocked = ", isLocked, " playerHasKey = ", playerHasKey(player))
if isLocked and not playerHasKey(player) then
If it doesn’t display what you’re expecting then you know where to go to find out what those variables are.
I’m guessing since you’ve got a lot of programming knowledge this is all basic information, but usually confirming the simplest items is where you start.
I’ve heard that certain variables don’t get updated from guis all that well. You could try adding a BoolValue, Attribute, or Tag directly to the player and not the gui to ensure it can be read from the server or the client.