GUI script sometimes (rarely) doesnt work

I have a local script in StarterGui that activates a gui when a player sits in a vehicle seat, to active mobile controls. It works 98% of the time but sometimes it doesnt register that the person is on mobile and won’t activate the control button gui. Any ideas why?

wait(1)
function activate()
	if game.Players.LocalPlayer.Character.Humanoid.SeatPart == nil then
		script.Parent.Enabled = false
		script.Parent.Car.Value = nil
		game.Players.LocalPlayer.TouchGui.Parent = game.Players.LocalPlayer.PlayerGui
		return
	end
	if game.Players.LocalPlayer.Character.Humanoid.SeatPart.ClassName == "VehicleSeat" then
		script.Parent.Enabled = true
		script.Parent.Car.Value = game.Players.LocalPlayer.Character.Humanoid.SeatPart.Parent
		game.Players.LocalPlayer.PlayerGui.TouchGui.Parent = game.Players.LocalPlayer
	end
end
local UIS = game:GetService("UserInputService")
local GUI = game:GetService("GuiService")

game.Players.LocalPlayer.Character.Humanoid.Changed:Connect(function(seat)
	if seat == "SeatPart" and UIS.TouchEnabled and not UIS.KeyboardEnabled and not UIS.MouseEnabled and not UIS.GamepadEnabled and not GUI:IsTenFootInterface() then
		activate()
	end
end)

Why you need a wait at the start of the script, do the script throws an error?

I recommend you to only use UIS.TouchEnabled because you can connect your keyboard, controller, etc., to your mobile device and the TouchGui will continue in there.

The wait is to make sure the character has loaded in before it starts… and there are no errors, the script works 98% of the time. I am unsure as to why sometimes it doesnt work when the character sits in the car, but it happens when someone else enters the server.

I have thought of this, but is this causing the issue?

And it continue happening all the times the player’s restart their character, and instead of using a wait use:

local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
1 Like

I will try this out, i had a feeling i need to get the service first.

Issue is hard to replicate so it will take some time before i know this works.

It’s not wrong with that, but maybe with the getting of Humanoid and Character, so do this:

local player = game.Players.LocalPlayer

player.CharacterAdded:connect(character)
local humanoid = character:waitForChild 'Humanoid'
function activate()
	if humanoid.SeatPart == nil then
		script.Parent.Enabled = false
		script.Parent.Car.Value = nil
	 	player.TouchGui.Parent = 		player.PlayerGui
		return
	end
	if humanoid.SeatPart.ClassName == "VehicleSeat" then
		script.Parent.Enabled = true
		script.Parent.Car.Value = player.Character.Humanoid.SeatPart.Parent
		player.PlayerGui.TouchGui.Parent = player
	end
end
local UIS = game:GetService("UserInputService")
local GUI = game:GetService("GuiService")

humanoid.Changed:Connect(function(seat)
	if seat == "SeatPart" and UIS.TouchEnabled then
		activate()
	end
end)
end)

Everytime the character of the player respawns or spawn this function will connect this.

Notice that I’m from my cellphone answering.

You can correct the typography when pasting it, but now you got the concept.

1 Like

Oh, and I forgot to place the UIS and GUI outside of the function, do it.

1 Like