Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

I want to add a button that substitutes for a key for mobile players. How do I do this?

My tool has a function where they can press the R key at any time to upgrade their tool for a cost. The vast majority of my friends do not have computers, which led me to thinking about how I could add a button that upgrades the tool when they can't really press R. I want to add something along the lines of a button that only appears when holding the tool, but does the same thing as the R key would on the computer. Any ideas? My current script is below:

repeat wait() until game.Players.LocalPlayer.Character
local Player = game.Players.LocalPlayer
local Body = Player.Character
local Mouse = Player:GetMouse()
local Level = script.Parent.Level.Value
local Cash = Player.leaderstats.Cash
local Tool = script.Parent
local Equipped
script.Parent.Activated:connect(function()
if not Equipped then return end
local Destination = Mouse.Hit
wait(5 - Level)
Body:MoveTo(Destination.p)
end)
Mouse.KeyDown:connect(function(k)
if not Equipped then return end
k = k:lower()
if k == "r" then
if Level < 5 then
if Cash.Value >= (Level * 5) then
Cash.Value = Cash.Value - (Level * 5)
Level = Level + 1
end
end
end
end)
Tool.Equipped:connect(function()
Equipped = true
end)
Tool.Unequipped:connect(function()
Equipped = false
end)

1 answer

✓ Accepted answer

You can check if they have Touch enabled:

game:GetService("UserInputService").TouchEnabled

will return a boolean based on whether or not the platform they're playing on is a touchscreen one or not. You can create a gui that contains the button which is shown whenever you equip the tool and the user has TouchEnabled. The button would execute the function that the "r" key currently does.

This discussion is preserved as it was. New answers, comments, and votes are closed.