Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

GUI Button That When Clicked, The Key "G" is Pressed? (UserInputService Problem)

Asked by 7 years ago
Edited 7 years ago

This was a continuation of my last question, but regardless, what the script is suppose to do is whenever this tool is Equipped, the GUI will be cloned, and from there, when the button is clicked, it will be equivalent to the G Key on the keyboard. Once it does that, it's suppose to do an animation. I have a link down at the end of these scripts.

I also have two other Local Scripts that are in relation to each other.

Here are the first two.

local tool=script.Parent
local gui=tool.PowerGui

tool.Equipped:connect(function()
    tool.rMovement.Disabled=true
    tool.lMovement.Disabled=true
    local Player=game.Players:findFirstChild(tool.Parent.Name)
    if Player and tool.Parent:findFirstChild("Humanoid") then
        local c=gui:Clone()
        c.Parent=Player.PlayerGui
        c.Frame.Script.Disabled=false
    end
    if tool.HandValue.Value==1 then
        tool.lMovement.Disabled=false
    else tool.rMovement.Disabled=false
    end
end)

What this first script does is that it clones the first GUI, this GUI has no problems, it's working for me perfectly. (In this case it is called PowerGui)

I have a script that is powering this GUI. This is the script:

local Player = script.Parent.Parent.Parent.Parent
local ScreenGui = script.Parent.Parent
local Frame = script.Parent

while true do
    wait()
    if Player:IsA("Player") then
        if Player.Character:findFirstChild("ABL_Ball") then
            Frame.Power.Text=Player.Character.ABL_Ball.PowerValue.Value
            if Player.Character.ABL_Ball.HandValue.Value==0 then
                Frame.Hand.Text="R"
            else Frame.Hand.Text="L"
            end
        else break
        end
    else break
    end
end
ScreenGui:Destroy()

So, what this does, it's a basketball, and there is a PowerValue, and a HandValue. This ball, whenever it is released, has a certain power people can use it on. The HandValue, is whatever the basketball is being dribbled. This basically occurs whenever a key on the keyboard is pressed. In this case, H/L. As you can see, at the very end, it destroys the GUI. This happens whenever the tool is unequipped, or it is being released.

My problem here are these two scripts.

local tool=script.Parent
local gui=tool.ControlsGui

tool.Equipped:connect(function()
    tool.rMovement.Disabled=true
    tool.lMovement.Disabled=true
    local Player=game.Players:findFirstChild(tool.Parent.Name)
    if Player and tool.Parent:findFirstChild("Humanoid") then
        local c=gui:Clone()
        c.Parent=Player.PlayerGui
        c.TextButton.LocalScript.Disabled=false
    end
    if tool.HandValue.Value==1 then
        tool.lMovement.Disabled=false
    else tool.rMovement.Disabled=false
    end
end)

This is equivalent to the very first one, in fact, I'm pretty sure it works, but I want it to be perfect according to this script below:

local tool =  script.Parent.Parent.Parent
local gui = script.Parent.Parent
local uis = game:GetService("UserInputService")
local button = script.Parent

tool.Equipped:connect(function() uis.InputBegan:connect(function(input) --code end) end)
    function Click()
        if input.UserInputType == Enum.KeyCode.G then
            local keyPressed = input.KeyCode
        end
    end
end)

tool.Unequipped:connect(function() gui.Visible = false end)

script.Parent.MouseButton1Click:connect(Click)

end)

I've tried and tried, I feel like I'm missing something, but I don't know what it is. This basically is inside the GUI. Whenever the tool is equipped, I am able to do a function or a certain type of code. When the button is clicked, it's equivalent to pressing the "G" key on the keyboard.

I've really tried very hard to figure out my mistakes, but there's so much I still need to learn in order to become the very best scripter I can possibly be.

Here's the Link for visual reference: Link to Visual Reference

Thank you, if you actually read through or at least skimmed, thank you (: If you can help I will give an upvote and will accept any answers given (just have it be thorough enough for me to understand as a beginning scripter lol)

Thanks, LukeGabrieI aka EnergyBrickz

A script would be helpful! c:

It gave me errors like,

"Passed value is not a function" (Looked it up. it has to do with the Click Function I tried to incorporate.)

0
To explain the error you encountered: In your last quoted script, thanks to the partially commented out line 6, the Click function is thus inside a function that isn't run until after line 16 (and only after InputBegan occurs). Thus, on line 16, `Click` is still 'nil'. chess123mate 5873 — 7y

1 answer

Log in to vote
2
Answered by 7 years ago

Not exactly sure what you tried doing but here's a set up I would use. The function Start() will be called when they press the gui or click G while holding the tool. Put what you wish to execute in the function.

local tool =  script.Parent.Parent.Parent
local gui = script.Parent.Parent
local button = script.Parent
local Debounce = true
local Active = false

local CoolDown = 1 -- Change to what you want
local function Start()
    -- Start animation and do whatever
end

tool.Equipped:Connect(function() 
    Active = true
  gui.Visible = false
end)

tool.Unequipped:Connect(function() 
    Active = false
    gui.Visible = false 
end)

script.Parent.MouseButton1Down:Connect()
    if Debounce then 
    Debounce = false
        Start()
    Wait(CoolDown)
    Debounce = true
  end
end)


function onKeyPress(actionName, userInputState, inputObject)
    if userInputState == Enum.UserInputState.Begin then
        if Active and Debounce then
        Debounce = false
            Start()
        Wait(CoolDown)
        Debounce = true
    end
    end
end

game.ContextActionService:BindAction("keyPress", onKeyPress, false, Enum.KeyCode.G)
0
Sorry for weird spacing idk why it showed up like that on here iamnoamesa 674 — 7y
Ad

Answer this question