Hello, wanted to know how to optimizate Mobile-Buttons what actually on equip and unequip deleting, i wanted to do connections but ZERO errors, and after equip- unequip not working
part of script with connections:
---------------------------------------------------------------------------------
local gunHUD = plr.PlayerGui:WaitForChild("GunHUD")
wait()
local mobilecontrols = gunHUD:WaitForChild("MobileKeys")
local Buttons = {
Aim = mobilecontrols:WaitForChild("Aim"),
Fire = mobilecontrols:WaitForChild("Fire"),
Inspect = mobilecontrols:WaitForChild("Inspect"),
Reload = mobilecontrols:WaitForChild("Reload")
}
local buttonConnections = {}
local function disconnectButtonEvents()
for _, connections in pairs(buttonConnections) do
for _, connection in ipairs(connections) do
connection:Disconnect()
end
end
buttonConnections = {}
end
local function onButtonPressed(button)
print("Mobile-Host buttonpressed")
if button == Buttons.Aim then
toggleAiming()
elseif button == Buttons.Fire then
isFiring = true
startFiring()
elseif button == Buttons.Inspect then
inspect()
elseif button == Buttons.Reload then
if ammoValue ~= maxAmmo then
startReloading()
end
end
end
local function onButtonPressedUp(button)
print("Mobile-Host buttonpressed")
if button == Buttons.Fire then
isFiring = false
end
end
local function connectButtonEvents()
disconnectButtonEvents() --delete them
for action, button in pairs(Buttons) do
buttonConnections[action] = {}
table.insert(buttonConnections[action], button.MouseButton1Down:Connect(function()
onButtonPressed(button)
end))
table.insert(buttonConnections[action], button.MouseButton1Up:Connect(function()
onButtonPressedUp(button)
end))
end
end
--main hud upd
local function refreshHUD()
gunHUD = plr.PlayerGui:WaitForChild("GunHUD")
mobilecontrols = gunHUD:WaitForChild("MobileKeys")
Buttons = {
Aim = mobilecontrols:WaitForChild("Aim"),
Fire = mobilecontrols:WaitForChild("Fire"),
Inspect = mobilecontrols:WaitForChild("Inspect"),
Reload = mobilecontrols:WaitForChild("Reload")
}
connectButtonEvents() --and connect them
end
tool.Unequipped:Connect(function()
refreshHUD()
end)
connectButtonEvents()
local plrGui = plr:WaitForChild("PlayerGui")
local gunHUD = plrGui:WaitForChild("GunHUD")
local mobilecontrols = gunHUD:WaitForChild("MobileKeys")
local Buttons = {
Aim = mobilecontrols:WaitForChild("Aim"),
Fire = mobilecontrols:WaitForChild("Fire"),
Inspect = mobilecontrols:WaitForChild("Inspect"),
Reload = mobilecontrols:WaitForChild("Reload")
}
local buttonConnections = {}
local function onButtonPressed(button)
print("Mobile-Host buttonpressed")
if button == Buttons.Aim then
toggleAiming()
elseif button == Buttons.Fire then
isFiring = true
startFiring()
elseif button == Buttons.Inspect then
inspect()
elseif button == Buttons.Reload then
if ammoValue ~= maxAmmo then
startReloading()
end
end
end
local function onButtonPressedUp(button)
print("Mobile-Host buttonpressed")
if button == Buttons.Fire then
isFiring = false
end
end
tool.Equipped:Connect(function()
for buttonName, button in Buttons do
local connections = {}
table.insert(connections, button.InputBegan:Connect(function()
onButtonPressed(button)
end))
table.insert(connections, button.InputEnded:Connect(function()
onButtonPressedUp(button)
end))
buttonConnections[buttonName] = connections
end
end)
tool.Unequipped:Connect(function()
for buttonName, button in Buttons do
for _, connection in buttonConnections[buttonName] do
connection:Disconnect()
end
buttonConnections[buttonName] = nil
end
end)
Since you’re using the buttons for mobile, using InputBegan and InputEnded instead of MouseButton1Down and MouseButton1Up should fix the issue. I’ve edited the code to reflect this
If the code above also fails to work, you can try this:
local plrGui = plr:WaitForChild("PlayerGui")
local gunHUD = plrGui:WaitForChild("GunHUD")
local mobilecontrols = gunHUD:WaitForChild("MobileKeys")
local Buttons = {
Aim = mobilecontrols:WaitForChild("Aim"),
Fire = mobilecontrols:WaitForChild("Fire"),
Inspect = mobilecontrols:WaitForChild("Inspect"),
Reload = mobilecontrols:WaitForChild("Reload")
}
local buttonConnections = {}
local function onButtonPressed(button)
print("Mobile-Host buttonpressed")
if button == Buttons.Aim then
toggleAiming()
elseif button == Buttons.Fire then
isFiring = true
startFiring()
elseif button == Buttons.Inspect then
inspect()
elseif button == Buttons.Reload then
if ammoValue ~= maxAmmo then
startReloading()
end
end
end
local function onButtonPressedUp(button)
print("Mobile-Host buttonpressed")
if button == Buttons.Fire then
isFiring = false
end
end
tool.Equipped:Connect(function()
for buttonName, button in Buttons do
buttonConnections[buttonName] = button:GetPropertyChangedSignal("GuiState"):Connect(function()
if button.GuiState == Enum.GuiState.Press then
onButtonPressed(button)
elseif button.GuiState == Enum.GuiState.Idle then
onButtonPressedUp(button)
end
end)
end
end)
tool.Unequipped:Connect(function()
for buttonName, button in Buttons do
if buttonConnections[buttonName] then
buttonConnections[buttonName]:Disconnected()
buttonConnections[buttonName] = nil
end
end
end)