I am making a drum set that, when you press a key, plays a sound. I am trying to adapt this to work for mobile using buttons on screen. Currently this is the code I have for keyboard which is a local script located within StarterPlayerScripts that tells a server script to play a sound using RemoteEvents.
-- FOLLOWING VARIABLES ARE ALL VARIABLES THAT APPEAR IN THE ENTIRE SCRIPT (to lazy to sort and find which ones the function uses :p) --
-- Player Info
local player = game:GetService("Players").LocalPlayer
local character = player.CharacterAdded:Wait()
local humanoid = character:WaitForChild("Humanoid")
controller = false
mobile = false
keyboard = true
-- Drum Info
local inputService = game:GetService("UserInputService")
local drumSounds = game.ReplicatedStorage.Drums
local state = true
pedal = false
-- Gui Info
local GUI = game.Players.LocalPlayer.PlayerGui.Drums
local keyboardGUI = GUI.PC
local consoleGUI = GUI.Console
local mobileGUI = GUI.Mobile
inputService.InputBegan:Connect(function(input)
if not state then
-- Following if statements send signal to server when user clicks key input
if pedal == false and input.KeyCode == Enum.KeyCode.J then
drumSounds.OpenHi:FireServer()
end
if pedal == true and input.KeyCode == Enum.KeyCode.J then
drumSounds.CloseHi:FireServer()
end
if input.KeyCode == Enum.KeyCode.K then
drumSounds.Ride:FireServer()
end
if input.KeyCode == Enum.KeyCode.N then
drumSounds.Tom1:FireServer()
end
if input.KeyCode == Enum.KeyCode.C then
drumSounds.Tom2:FireServer()
end
if input.KeyCode == Enum.KeyCode.L then
drumSounds.TomF:FireServer()
end
if input.KeyCode == Enum.KeyCode.A then
drumSounds.Snare:FireServer()
end
if input.KeyCode == Enum.KeyCode.B then
drumSounds.Kick:FireServer()
end
end
end)
Now my plan was to do something similar for mobile but I don’t want do write and entire new function if I don’t have to. From my knowledge (still a beginner coder) the only way I know of doing this would to do something such as GUIbutton.Activated:Connect(function()) or putting a script into every single Button and putting script.Parent.MouseButton1Click:Connect(function())
So to repeat myself: is there a better way to tell when a player interacts with a GUI Button that isn’t just an entire separate function for each button?