Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

Why do I have to sit twice for the GUI to show up?

The script is inside the VehicleSeat, but I have to sit twice in order for the GUI to show up.

local ui = script.Parent:FindFirstChild("PoliceGUI")

script.Parent.Touched:connect(function(hit)
	if hit.Parent ~= nil then
		local player = game.Players:GetPlayerFromCharacter(hit.Parent)
		if hit.Parent:FindFirstChild("Humanoid") ~= nil then
			hit.Parent.Humanoid.Seated:connect(function(active, seat)
				if player ~= nil then
					if active == true and seat == script.Parent then
						if player.PlayerGui:FindFirstChild("PoliceGUI")== nil then
							ui:Clone().Parent = player.PlayerGui
							print("seated")
						end
					elseif active ~= true and seat ~= script.Parent then
						if player.PlayerGui:FindFirstChild("PoliceGUI") ~= nil then
							player.PlayerGui:FindFirstChild("PoliceGUI"):Destroy()
							print("not seated")
						end
					end
				end
			end)
		end
	end
end)

2 answers

Remake of script:


--[[ Services ]]--
local Players = game:GetService("Players")

--[[ Instances ]]--
local Seat = script.Parent
local UI = Seat:FindFirstChild("PoliceGUI")

--[[ Variables ]]--
local Player

--[[ Events ]]--
Seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if Player then
		if Player.PlayerGui:FindFirstChild("PoliceGUI") then
			Player.PlayerGui.PoliceGUI.Visible = false
		end
	end
	if Players:GetPlayerFromCharacter(Seat.Occupant) then
		Player = Players:GetPlayerFromCharacter(Seat.Occupant)
		if not Player.PlayerGui:FindFirstChild("PoliceGUI") then
			UI:Clone().Parent = Player.PlayerGui
		end
		Player.PlayerGui.PoliceGUI.Visible = true
	end
end)

Explanation coming later,

2 comments

0

It didn't work

0

What didn't work

Is it possible the PoliceGui is already inside the PlayerGui but invisible? Do a double check and make sure PoliceGui isn't already in StarterGui.

2 comments

0

Change to comment pls

0

It's not in the StarterGui, it's in the VehicleSeat

This discussion is preserved as it was. New answers, comments, and votes are closed.