What am I attempting to achieve?
I am attempting to make a consistently functioning upgrade button. The upgrades can be purchased by right clicking, or a hotkey.
What is the Issue?
My issue is that the hotkey will sometimes not work(half the time?) when trying to upgrade, however, using right click will always work, as shown in the GIF.
Upgrade Script
function onKeyPress(inputObject, gameProcessedEvent)
if debounce == false and teamName == "Green" and inputObject.KeyCode == Enum.KeyCode.Two and script.Parent.Parent.Parent.Enabled == true and RedUpgrades:FindFirstChild(UpgradeType).Value < 3 and Cost.Value <= Cash.Value then
debounce = true
GreenUpgradeEvent:FireServer(Cost.Value,UpgradeType)
print("eh??")
if GreenUpgrades:FindFirstChild(UpgradeType).Value == 0 then
Cost.Value = 185
elseif GreenUpgrades:FindFirstChild(UpgradeType).Value == 1 then
Cost.Value = 270
elseif GreenUpgrades:FindFirstChild(UpgradeType).Value == 2 then
Cost.Value = 0
end
wait(.7)
debounce = false
elseif debounce == false and teamName == "Red" and inputObject.KeyCode == Enum.KeyCode.Two and script.Parent.Parent.Parent.Enabled == true and RedUpgrades:FindFirstChild(UpgradeType).Value < 3 and Cost.Value <= Cash.Value then
debounce = true
RedUpgradeEvent:FireServer(Cost.Value,UpgradeType)
print("eh??")
if RedUpgrades:FindFirstChild(UpgradeType).Value == 0 then
Cost.Value = 185
elseif RedUpgrades:FindFirstChild(UpgradeType).Value == 1 then
Cost.Value = 310
elseif RedUpgrades:FindFirstChild(UpgradeType).Value == 2 then
Cost.Value = 0
end
wait(.7)
debounce = false
end
end
game:GetService("UserInputService").InputBegan:connect(onKeyPress)
GIF and GUI Hierarchy
Before I moved my mouse over and clicked, I used the hotkey several times, however the upgrade did not change until I right clicked.
Gyazo GIF
Upgrade ButtonHierarchy:
What solutions have I tried so far?
I tried using the output to see if there errors, and seeing if anything was weird with my variables, however I could find anything that pointed to a solution to the issue.
I apologize if my script is poorly made, please criticize if necessary. Any advice would be very helpful. Also, if I left anything out in this post, please let me know.
Thanks!