I want the gui to only appear on the player who is seated
Can you help?
I’ve tried some solutions but it didn’t work.
Here is my code:
local Passpart = game.Workspace.PassPart
local prox = Passpart.ProximityPrompt
prox.Triggered:Connect(function(plr)
if plr.Backpack:FindFirstChild("Pasaport") then
print("Pasaport found!")
local Seat = game.Workspace.SMSEAT
local function Seated()
if Seat.Occupant ~= nil then
game.StarterGui["Pasaport Gui"].Passaport.Visible = true
end
if Seat.Occupant == nil then
game.StarterGui["Pasaport Gui"].Passaport.Visible = false
end
end
Seat:GetPropertyChangedSignal("Occupant"):Connect(Seated)
else
print("Pasaport not found")
end
end)
local Passpart = game.Workspace.PassPart
local prox = Passpart.ProximityPrompt
-- It is better to store such variables behind connections
local Seat = workspace.SMSEAT
prox.Triggered:Connect(function(plr)
if plr.Backpack:FindFirstChild("Pasaport") then
print("Pasaport found!")
Seat:GetPropertyChangedSignal("Occupant"):Once(function()
-- StarterGui is the basis from which players take their Gui when respawning
-- PlayerGui is a UI repository for each player individually. Every player has it and displays their UI in real time.
local UI = plr.PlayerGui["Pasaport Gui"].Passaport
-- when you enter if Seat.Occupant without using other conditional statements,
-- it automatically checks whether the element is nil or false,
-- if not then the condition continues if yes, then it is loaded or continues in else if you have one.
if Seat.Occupant then
-- for example:
-- UI.Visible = true
-- not true = false
-- if not true => if true == false
-- if not UI.Visible => if UI.Visible == false then UI.Visible = true
if not UI.Visible then
UI.Visible = true
end
-- if true == true then
elseif UI.Visible then
UI.Visible = false
end
end)
else
print("Pasaport not found")
end
end)
No, it’s not necessary here. The line below does this.
we check if Seat.Occupant is not equal to nil, then in the next check we check whether our UI is enabled. Elseif fires when Seat.Occupant is nil and turns off the UI.
elseif replaces this with the following construction:
if Seat.Occupant then
if not UI.Visible then
UI.Visible = true
end
else
if UI.Visible then
UI.Visible = false
end
end
The console is empty because you are showing the client side.
Client is everything that is displayed by the player and is subjective to the player.
The server is all you usually see.
That is, the world (workspace). Your script is on the server side, so you can’t see what it outputs on the client. Change the console to a server side.
local seat = script.Parent
local guiSource = seat:WaitForChild("ScreenGui")
local gui = nil
seat:GetPropertyChangedSignal("Occupant"):Connect(function()
if seat.Occupant then
local player = game.Players.LocalPlayer
if gui then
gui:Destroy()
gui = nil
end
gui = guiSource:Clone()
gui.Parent = player.PlayerGui
else
if gui then
gui:Destroy()
gui = nil
end
end
end)