I’m trying to make a mobile ability button for my new fanmade game of Slap Battles (usually common nowadays ) 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.
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.
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?
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.
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.
local Players = game:GetService("Players")
local player = Players.LocalPlayer -- this only works in a LocalScript
local playerGui = player:WaitForChild("PlayerGui")
local player = game.Players.LocalPlayer
local gui = player:WaitForChild('PlayerGui'):WaitForChild('AbilityGui')
local cooldownLabel = gui.TextButton.CooldownLabel
cooldownLabel.Text = 'New text'