So I was making a mobile tool that uses context action for its buttons.
When the player equips the tool, it will bind the actions first from a table of fixed strings.
After that, it will check the player’s TouchEnabled and if it is true, button will be Getbutton()ed and it will be set to a certain position.
When the player unequips the tool, it will unbind all action strings from the table.
However, the mobile buttons only showed up in the first time when I equip the tool. When I unequip the tool and equip it again, the buttons do not show up although from the PlayerGui, I can see them over there.
When I click on the explorer tab of the buttons’ shared parent of a Frame, they will show up on screen.
Here is my code:
local CAconnections = {
Fire = "Fire",
Reload = "Reload",
ChangeMode = "CM"
}
gun.Equipped:Connect(function()
Equipped = true
ContextAction:BindAction(
CAconnections.Reload,
function()
Reload()
end,
true,
Enum.KeyCode.R
)
ContextAction:BindAction(
CAconnections.ChangeMode,
function()
Changed_Firemode()
end,
true,
Enum.KeyCode.V
)
if UserInput.TouchEnabled == true then
local CMButton = ContextAction:GetButton(CAconnections.ChangeMode)
print(CMButton)
ContextAction:SetTitle(CAconnections.ChangeMode, "Change-mode")
CMButton.Active = false
CMButton.AnchorPoint = Vector2.new(0.5,0.5)
CMButton.Size = UDim2.fromScale(0.2,0.2)
CMButton.Position = UDim2.fromScale(0.3,0.35)
local SizeConstrait = Instance.new("UIAspectRatioConstraint",CMButton)
SizeConstrait.AspectRatio = 1
local ReloadButton = ContextAction:GetButton(CAconnections.Reload)
print(ReloadButton)
ContextAction:SetTitle(CAconnections.Reload, "Reload")
ReloadButton.Active = false
ReloadButton.AnchorPoint = Vector2.new(0.5,0.5)
ReloadButton.Size = UDim2.fromScale(0.2,0.2)
ReloadButton.Position = UDim2.fromScale(0.35,0.15)
local SizeConstrait = Instance.new("UIAspectRatioConstraint",ReloadButton)
SizeConstrait.AspectRatio = 1
ContextAction:BindAction(
CAconnections.Fire,
function()
if Variables.Loaded_Rounds > 0 then
Press_Trigger()
end
end,
true,
Enum.KeyCode.P
)
local Fire_Button = ContextAction:GetButton(CAconnections.Fire)
ContextAction:SetTitle(CAconnections.Fire, "Fire")
print(Fire_Button)
Fire_Button.Active = false
Fire_Button.AnchorPoint = Vector2.new(0.5,0.5)
Fire_Button.Size = UDim2.fromScale(0.3,0.3)
Fire_Button.Position = UDim2.fromScale(0.55,0.3)
local SizeConstrait = Instance.new("UIAspectRatioConstraint",Fire_Button)
SizeConstrait.AspectRatio = 1
EventKey.LeftButtonDown = Fire_Button.InputBegan:Connect(function()
LD = true
end)
EventKey.LeftButtonUp = Fire_Button.InputEnded:Connect(function()
LD = false
end)
else
EventKey.LeftButtonDown = mouse.Button1Down:Connect(function()
wait(Data.Config.Fire_Delay)
Burst_left = Data.Config.BurstCount
LD = true
if Variables.Loaded_Rounds > 0 then
Press_Trigger()
end
end)
EventKey.LeftButtonUp = mouse.Button1Up:Connect(function()
LD = false
end)
end
end)
gun.Unequipped:Connect(function()
Equipped = false
for i,v in pairs(CAconnections) do
local Button = ContextAction:GetButton(v)
Button:Destroy()
ContextAction:UnbindAction(v)
end
end)
There will be the same problem if I do not destroy the buttons.