Issue with mobile ability button

I’m trying to make a mobile ability button for my new fanmade game of Slap Battles (usually common nowadays :laughing:) but my ability button won’t work.

I tried searching up what TouchTap, Mousebutton1click, and Activated do for the E button. So far, none of them have worked and mobile players reported that their abilities would not fire.

Here is the code: (Might be a bit large)

local userInputService = game:GetService("UserInputService")
local isMobile = userInputService.TouchEnabled

if isMobile == true then
	script.Parent.Visible = true
else
	script.Parent.Visible = false
end

local player = game.Players.LocalPlayer

local abilityButton = script.Parent


abilityButton.MouseButton1Click:Connect(function()
	print("Mobile ability button pressed.")
	
	local glove = game.Players.LocalPlayer:FindFirstChild("leaderstats"):WaitForChild("Glove")
	local tool = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Tool")
	
	local event = game:GetService("ReplicatedStorage"):WaitForChild("AbilityEvent")
	local buttonText = script.Parent.Text
	local label = game.StarterGui.AbilityGui.TextButton.CooldownLabel

	if glove.Value == "Default" and tool:FindFirstChild(glove.Value) then -- Default Glove
		local hitbox = tool:WaitForChild("Hitbox")
		local abilityName = hitbox:FindFirstChild("Ability").Value		
		
		local cooldown = 4
		local originalCooldown = 4
		label.Visible = true
		
		event:FireServer(glove)
		script.Disabled = true
		game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = abilityName
			
		while wait(0.1) do
			cooldown = cooldown - 0.1
			label.Size = UDim2.new(cooldown / originalCooldown, 0,1,0)
			if cooldown <= 0 then
				if player.Character:FindFirstChild("Humanoid") and player.Character:FindFirstChild("Humanoid").Health > 0 then
					label.Size = UDim2.new(0,0,1,0)
					label.Parent.Visible = false
					game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = "Ability"
					break
				else
					game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = "Ability"
					break
				end
			elseif player.Character:FindFirstChild("Humanoid") and player.Character:FindFirstChild("Humanoid").Health <= 0 then
				label.Size = UDim2.new(0,0,1,0)
				label.Parent.Visible = false
				game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = "Ability"
				break
			end
		end
		script.Disabled = false
	end
end)

If any of you can help, it’d be appriciated!
I need to fix this pretty bag.

2 Likes

What specifically happens? Does the ability not activate at all or does it activate incorrectly?

It doesn’t activate at all, even when being pressed correctly with any function. This includes TouchTap and Activated.

1 Like

Where is the script placed, since it’s a LocalScript.

The script is placed here, look in the screenshot.

game.StarterGui>MobileAbilityButton>TextButton>TouchTapScript

1 Like

I think it’s because you wrote this:

local label = game.StarterGui.AbilityGui.TextButton.CooldownLabel

StarterGui is just like the setup version of the UI. When the game runs, it makes a copy of it into PlayerGui, which is the one the player actually sees. So if you try to change stuff in StarterGui while the game is running, nothing will happen. You always gotta change PlayerGui instead.

Let me review that inside of the code and I will reply you the results.

1 Like

I tested out the results with a mobile playtester of mine. He said it did not fire. Why is that?
Here is the new code that has been created:

local userInputService = game:GetService("UserInputService")
local isMobile = userInputService.TouchEnabled

if isMobile == true then
	script.Parent.Visible = true
else
	script.Parent.Visible = false
end

local player = game.Players.LocalPlayer

local abilityButton = script.Parent


abilityButton.MouseButton1Click:Connect(function()
	print("Mobile ability button pressed.")
	
	local glove = game.Players.LocalPlayer:FindFirstChild("leaderstats"):WaitForChild("Glove")
	local tool = game.Players.LocalPlayer.Character:FindFirstChildOfClass("Tool")
	local event = game:GetService("ReplicatedStorage"):WaitForChild("AbilityEvent")

	if glove.Value == "Default" and tool:FindFirstChild(glove.Value) then -- Default Glove
		local buttonText = script.Parent.Text
		local label = game.StarterGui.AbilityGui.TextButton.CooldownLabel
		
		local hitbox = tool:WaitForChild("Hitbox")	
		local abilityName = hitbox:FindFirstChild("Ability").Value		
		
		local cooldown = 4
		local originalCooldown = 4
		label.Visible = true
		
		event:FireServer(glove)
		script.Disabled = true
		game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = abilityName
			
		while wait(0.1) do
			cooldown = cooldown - 0.1
			label.Size = UDim2.new(cooldown / originalCooldown, 0,1,0)
			if cooldown <= 0 then
				if player.Character:FindFirstChild("Humanoid") and player.Character:FindFirstChild("Humanoid").Health > 0 then
					label.Size = UDim2.new(0,0,1,0)
					label.Parent.Visible = false
					game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = "Ability"
					break
				else
					game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = "Ability"
					break
				end
			elseif player.Character:FindFirstChild("Humanoid") and player.Character:FindFirstChild("Humanoid").Health <= 0 then
				label.Size = UDim2.new(0,0,1,0)
				label.Parent.Visible = false
				game.Players.LocalPlayer.PlayerGui:WaitForChild("AbilityGui").TextButton.Text = "Ability"
				break
			end
		end
		script.Disabled = false
	end
end)

Have you been suggesting that I’d put it into the If check for default instead of before the checks?

2 Likes

Wait, I realized:
I’m doing it in startergui.

You said don’t do it in startergui, but always do it in playergui.
but here’s the problem.

I can’t change the AbilityGui’s label without going into game.startergui first.
Otherwise, it would be impossible to do tweenservice on the abilitygui’s label, causing the whole ability system to break.

1 Like

Yes don’t use StarterUI, because as I said before:

You have to change PlayerGUI.


How would I change AbilityGui.TextButton.CooldownLabel without going to game.StarterGui?
Isn’t that impossible?

1 Like

StarterGui is like the source file. PlayerGui is the one the player actually sees. So even if your GUI is set up in StarterGui, once the game runs, you have to use PlayerGui for changes like tweens and visibility, otherwise it’s like editing a draft nobody’s reading.

Blockquote So even if your GUI is set up in StarterGui, once the game runs, you have to use PlayerGui for changes like tweens and visibility, otherwise it’s like editing a draft nobody’s reading.

How would I access PlayerGui?

1 Like

Here:

local Players = game:GetService("Players")
local player = Players.LocalPlayer  -- this only works in a LocalScript
local playerGui = player:WaitForChild("PlayerGui")

You’re going to have to change it using scripts.

local player = game.Players.LocalPlayer
local gui = player:WaitForChild('PlayerGui'):WaitForChild('AbilityGui')
local cooldownLabel = gui.TextButton.CooldownLabel

cooldownLabel.Text = 'New text'

Would THIS work?

1 Like

yes

I think so. Try it and tell me if it does.

1 Like


Sadly, it doesn’t work. Am I doing it correctly?

Any errors in the console?

“F9”