Need help creating mobile button & connecting to script

  1. I’m trying to create a mobile button and connect it to a script.

  2. I cannot figure out how to create then connect a mobile button to a script inside my tool.

  3. I have looked around on Google and the Devfourm, I figured out a way to create a mobile button, but I’m still confused on how to connect it to my script.

Here is the main script inside the tool:

local tool = script.Parent
local anim = Instance.new("Animation")
local anim1 = Instance.new("Animation")
local anim2 = Instance.new("Animation")
local anim3 = Instance.new("Animation")
local blade = tool.blade
local bladein = tool.innerblade
local light = blade.PointLight
local light2 = blade.PointLightLarge
local eqpsnd = tool.Handle.Equip
local idlesnd = tool.Handle.Idle
local uneqpsnd = tool.Handle.Unequip
local swngsnd = tool.Handle.Slash
local swngsndbg = tool.Handle.SlashBig
local swngsndsm = tool.Handle.SlashSmall
local trail = tool.innerblade.Trail
local dmg = blade.Dmg
local fnshdmg = blade.FinishDmg
local Debounce = false


anim.Name = "IdleAnim"
anim.AnimationId = "rbxassetid://10164092283"
anim1.Name = "EquipAnim"
anim1.AnimationId = "rbxassetid://10164101710"
anim2.Name = "SlashAnim"
anim2.AnimationId = "rbxassetid://10164095647"
anim3.Name = "SlashBgAnim"
anim3.AnimationId = "rbxassetid://10164099818"
local track
local track1
local track2
local track3

-- When Tool Equipped
tool.Equipped:Connect(function()
	Debounce = true
	eqpsnd:Play()
	track = script.Parent.Parent.Humanoid:LoadAnimation(anim)
	track1 = script.Parent.Parent.Humanoid:LoadAnimation(anim1)
	track.Priority = Enum.AnimationPriority.Movement
	track1.Priority = Enum.AnimationPriority.Movement
	track.Looped = true
	track:Play()
	track1:AdjustSpeed(0.3)
	track1:Play()
	wait(0.8)
	idlesnd:Play()
	blade.Transparency = 0.1
	bladein.Transparency = 0.2
	light.Enabled = true
	light.Enabled = true
	Debounce = false
end)

-- When Tool UnEquipped
tool.Unequipped:Connect(function()
	if track and track1 then
		idlesnd:Stop()
		blade.Transparency = 1
		bladein.Transparency = 1
		light.Enabled = false
		light.Enabled = false
		uneqpsnd:Play()
		track:Stop()
		track1:Stop()
	end
end)

tool.Activated:Connect(function()
	if not Debounce then
		Debounce = true
		track2 = script.Parent.Parent.Humanoid:LoadAnimation(anim2)
		track2:Play()
		trail.Enabled = true
		swngsnd:Play()
		dmg.Disabled = false
		wait(0.6)
		dmg.Disabled = true
		trail.Enabled = false
		wait(0.3)
		Debounce = false
	end		
end)

 -- Trying to connect mobile button to this
script.Parent.KeyDown.OnServerEvent:Connect(function(player,key)
	if player ~= game.Players:GetPlayerFromCharacter(script.Parent.Parent) then return end
	local letter = key.Name
	letter = letter:lower()
	if letter == "f" then
		if not Debounce then
			Debounce = true
			track3 = script.Parent.Parent.Humanoid:LoadAnimation(anim3)
			track3:Play()
			script.Parent.Parent.Humanoid.WalkSpeed = 0
			trail.Enabled = true
			swngsndbg:Play()
			wait(0.5)
			fnshdmg.Disabled = false
			wait(1.2)
			fnshdmg.Disabled = true
			swngsndsm:Play()
			wait(1)
			trail.Enabled = false
			script.Parent.Parent.Humanoid.WalkSpeed = 16
			Debounce = false	
		end
	end
end)

You mean using ContextActionService to handle player input? I think I know what your talking about.
image

try this:

-- Trying to connect mobile button to this
function pressed(player,key)
	if player ~= game.Players:GetPlayerFromCharacter(script.Parent.Parent) then return end
	local letter = key.Name
	letter = letter:lower()
	if letter == "f" then
		if not Debounce then
			Debounce = true
			track3 = script.Parent.Parent.Humanoid:LoadAnimation(anim3)
			track3:Play()
			script.Parent.Parent.Humanoid.WalkSpeed = 0
			trail.Enabled = true
			swngsndbg:Play()
			wait(0.5)
			fnshdmg.Disabled = false
			wait(1.2)
			fnshdmg.Disabled = true
			swngsndsm:Play()
			wait(1)
			trail.Enabled = false
			script.Parent.Parent.Humanoid.WalkSpeed = 16
			Debounce = false	
		end
	end
end

local mobilebutton = ContextActionService:BindAction("Button",pressed,true,"press")
ContextActionService:SetPosition("Button",UDim2.new(0,0,0)) -- set the position
--ContextActionService:SetImage("Button","rbxassetid://youridhere")

also don’t forget to set the position of the button and optionally set up an image :slight_smile:
Hope this helps!

Also Im not sure why you are using a key function because I don’t know where the key comes from if the player pressed the button. I think this only makes sense if you are using the UserInputService…