My GUI disappears randomly, I think it has to do with the RunContext set to client

My GUI disappears randomly, this is a serverscript set to client runcontext. I don’t know why it disappears randomly. My initial thoughts are maybe because if you lag a bit it will just disappear. This code is for a Train GUI.

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")
local player = Players.LocalPlayer

local seat = script.Parent

function updateDisplay()

	local seat = script.Parent

	local char = seat.Occupant.Parent
	local occupantPlayer = game.Players:GetPlayerFromCharacter(char)

	local carGui = occupantPlayer.PlayerGui.TrainGui
	local mphCounter = carGui.MainFrame.Speed
	carGui.MainFrame.Speed.Text = `KPH: {script.Parent.Parent.Base.Power.LineVelocity}`
	carGui.MainFrame.SpeedFrame:TweenSize(UDim2.new(math.clamp(script.Parent.Parent.Base.Power.LineVelocity/100, 0, 1), 0, 0.08,0), Enum.EasingDirection.In, Enum.EasingStyle.Linear, 0.5, true)
end

local updateConnection

seat:GetPropertyChangedSignal("Occupant"):Connect(function()
	if seat.Occupant == player.Character.Humanoid then
		player.PlayerGui.TrainGui.Enabled = true
		updateConnection = RunService.RenderStepped:Connect(updateDisplay)
	elseif seat.Occupant ~= player.Character:WaitForChild("Humanoid") then
		player.PlayerGui.TrainGui.Enabled = false
		if updateConnection then
			updateConnection:Disconnect()
		end
	end
end)

1 Like

It’s possible that the GUI is disappearing because of a momentary lag spike or connection issue between the client and the server. One potential solution would be to add some error checking to the updateDisplay() function to ensure that the GUI is properly updated even if there is a momentary lag or connection issue. You could also include a try-catch statement to catch any errors that may occur during the execution of the function. Another possible solution would be to modify the updateConnection to only fire when the GUI is visible, so that the function won’t run unnecessarily and potentially cause issues with the GUI.

1 Like

That is not the issue, the updating is fine. I think it must be the function because its the only thing that makes the gui visible and invisible.

Why are you calling wait for child on the humanoid in a connected event? Why are you checking the seat’s occupant? You should be detecting if the character humanoid of the local player has sat down.

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.