Create mobile button alternative

Hi,
I have a NPC scrpt that allows players to walk up to the NPC and press E to begin a dialog gui. This works fine on a PC but on a mobile device there is no way to press the E key. How do i go about adding an alternative option for mobile players.

local UIS = game:GetService("UserInputService")

local Template = script.Parent:WaitForChild("Template")
local E = script.Parent.Parent:WaitForChild("DetectorPopUp"):WaitForChild("Key")

local Player = game:GetService("Players").LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local RootPart = Character:WaitForChild("HumanoidRootPart")

local VarHolder
local Text
local CurrentTrigger
local NPC
local db
local abletochange = true
local StoredValues = {}

local function DisableMove()
	if(Character.Humanoid.WalkSpeed~=0)then
		StoredValues = {Character.Humanoid.WalkSpeed,Character.Humanoid.JumpPower}
		Character.Humanoid.WalkSpeed = 0
		Character.Humanoid.JumpPower = 0
	end
end

local function EnableMove()
	Character.Humanoid.WalkSpeed = StoredValues[1]
	Character.Humanoid.JumpPower = StoredValues[2]
end

RootPart.Touched:Connect(function(otherPart)
	if(otherPart.Name=="Detector")then
		CurrentTrigger = otherPart.NPCTitle.Value
		E.Visible = true
		E.Parent.Adornee = otherPart
		NPC = otherPart.Parent.NPC
	end
end)

RootPart.TouchEnded:Connect(function(otherPart)
	if(otherPart.Name=="Detector")then
		CurrentTrigger = nil
		E.Visible = false
		E.Parent.Adornee = nil
		NPC = nil
	end
end)

UIS.InputBegan:Connect(function(inputObject, gameProcessedEvent)
	if(inputObject.KeyCode==Enum.KeyCode.E)and(not(db))then
		if(CurrentTrigger)then
			SetNewNpc()
		end
	end
end)

function SetNewNpc()
	if(abletochange)then
		VarHolder = NPC.Parent.Detector
		TextValue = VarHolder.Text
		Text = TextValue.Value
		SetNewTextBox()
	end
end

function SetNewTextBox()
	if(not(db))then
		db = true
		DisableMove()
		local TextBox = Template:Clone()
				TextBox.Parent = script.Parent
				TextBox.Text = ""
				TextBox.Size = UDim2.new(0,500,0,100)
				TextBox.Position = UDim2.new(0.5,-250,1,-150)
				TextBox.Visible = true
		for i=1,string.len(Text) do
			TextBox.Text = string.sub(Text, 1, i)
			wait(0.02)
		end
		UIS.InputBegan:Wait()
		TextBox:Destroy()
		if(TextValue:FindFirstChildOfClass("StringValue"))then
			VarHolder = TextValue
			TextValue = VarHolder.Text
			Text = TextValue.Value
			abletochange = false
			db = false
			SetNewTextBox()
		else
			EnableMove()
			wait(1)
			db = false
			abletochange = true
		end
	end
end

Take a look into ContextActionService: ContextActionService | Documentation - Roblox Creator Hub

This function allows you to to do something like the following code

local CAS = game:GetService("ContextActionService")

CAS:BindAction("TalkToNPC", function(actionName, inputState, inputObject)
  print(actionName, inputState, inputObject)
  if inputState == Enum.UserInputState.Begin then
    --They first pressed the button
  end
end, true, Enum.KeyCode.E) --The true creates a mobile button, the Enum.KeyCode.E is the key you want to be pressed on the computer

This is placed in the StarterPlayerScripts?
How do i go about making this interact with the NPC dialog script?
When this is added to StarterPlayerScripts, it creates a button for mobile device but when clicked it returns InteractinputObject to the output window. Is it simulating the E button being pressed or do i need to edit the NPC dialog script to receive the interaction?

You would replace your UIS.InputBegan with this function, and put the code from that, into this. It will take a little conversion as you don’t need to check if the key is the correct key.

Pressing the button on mobile just fires the function. Note though, this will make a UI button on the screen at all times unless you bind it when they go in range and unbind when out of range.

If you want it to have them press E or tap the NPC to open a dialogue, I would using ContextActionService still, then if the device is mobile, having the pop up saying to press E, instead be a TextButton saying “Tap here” or something of the sort.

1 Like

Thank you, makes more sense now.