Hello devforum, I need some help with mobile buttons in a weapon. I am making an aim system. It works well, but I want to change it. Instead of making the player aim while holding the button, I want the button to toggle the aim (only in mobile). You click once, it activates, you click again it deactivates. And I am not sure how to do that. Here’s how I am doing it right now:
contextActionService:BindAction("Aim", aim, true, Enum.UserInputType.MouseButton2)
contextActionService:SetPosition("Aim", UDim2.new(0.250, 0,0.603, 0))
function aim()
if not isMobile then
if aiming == false or aiming == nil then
mouse.Icon = newAimIcon
camera.FieldOfView = aimFieldOfView
aiming = true
else
mouse.Icon = newNormalIcon
aiming = false
camera.FieldOfView = defaultFieldOfView
end
else
if aiming == false or aiming == nil then
aiming = true
camera.FieldOfView = aimFieldOfView
mobileMouse.Image = "http://www.roblox.com/asset/?id=138864934"
else
aiming = false
camera.FieldOfView = defaultFieldOfView
mobileMouse.Image = "http://www.roblox.com/asset/?id=138865017"
end
end
end
Your current script doesn’t check the inputstate. I added a check for that in the mobile related part of it. It should now only start or stop aiming when the player releases the button, which means only one aiming change (either activation or deactivation) will be done when the player presses the button. Does it now work?
contextActionService:BindAction("Aim", aim, true, Enum.UserInputType.MouseButton2)
contextActionService:SetPosition("Aim", UDim2.new(0.250, 0,0.603, 0))
function aim(_, inputState)
if not isMobile then
if not aiming then
mouse.Icon = newAimIcon
camera.FieldOfView = aimFieldOfView
aiming = true
else
mouse.Icon = newNormalIcon
aiming = false
camera.FieldOfView = defaultFieldOfView
end
else
if inputState ~= Enum.UserInputState.End then
return
end
if not aiming then
aiming = true
camera.FieldOfView = aimFieldOfView
mobileMouse.Image = "http://www.roblox.com/asset/?id=138864934"
else
aiming = false
camera.FieldOfView = defaultFieldOfView
mobileMouse.Image = "http://www.roblox.com/asset/?id=138865017"
end
end
end
The action name string, inputstate enum and inputobject are automatically set as the values of parameters when the function is called (when the input happens). The code didn’t work because I forgot the actionname, which is set as the first parameter’s value. The inputstate is actually the second parameter’s value. I have now added a placeholder parameter for the name to the code in my earlier reply, so the inputstate parameter should now actually get the inputstate as its value. Does the code now work?