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

I made it so whenever I press X I can then press left click then shoot. Not working as I intended?

Asked by
OFF_S4LE 127
3 years ago

So, I made it so whenever I press X I can then press left click then shoot. When I press X I can shoot all I want. How would I make it so when I press X I can only shoot once then I have to press X again.

Code:

function onKeyPress(actionName, userInputState, inputObject)
    local debounce = false
    if userInputState == Enum.UserInputState.Begin then
        print("X was pressed")
        local debounce = false

        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()

        script.Parent.Activated:Connect(
            function()
                if not debounce then
                    debounce = true

                    script.RemoteEvent:FireServer(mouse.Hit)
                    local hum = player.Character.Humanoid

                    for i = 1, 15 do
                        wait()
                        hum.CameraOffset = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))
                    end
                    hum.CameraOffset = Vector3.new(0, 0, 0)

                    wait(2)
                    debounce = false
                end
            end
        )
    end
end

game.ContextActionService:BindAction("keyPressSpecialName", onKeyPress, true, Enum.KeyCode.X)
game.ContextActionService:SetPosition("keyPressSpecialName", UDim2.new(.5, 0, -.5, 0))
game.ContextActionService:SetImage("keyPressSpecialName", "rbxassetid://73737626")
0
Where is the error? gamerpenguinpvp34 2 — 3y
0
I recognize this name from somewhere, hmmm... User#30567 0 — 3y
0
hehe, i use to use scripting helpers alot OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
View all comments (33 more)
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
also i only play dungeon quest on this account OFF_S4LE 127 — 3y
0
did you spam post lo User#30567 0 — 3y

1 answer

Log in to vote
0
Answered by 3 years ago
Edited 3 years ago

this is a lazy answer, there's probably a better one incoming but here's my lazy fix

first we define a variable that's called shotFired. this will be used to detect if the shot has been fired already. set it to false, and place it anywhere before the Activated event.

-- Sliced off 5 lines
        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()
    local shotFired = false -- define variable

        script.Parent.Activated:Connect(
            function()
                if not debounce then
                    debounce = true

                    script.RemoteEvent:FireServer(mouse.Hit)
                    local hum = player.Character.Humanoid

                    for i = 1, 15 do
                        wait()
                        hum.CameraOffset = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))
                    end
                    hum.CameraOffset = Vector3.new(0, 0, 0)

                    wait(2)
                    debounce = false
                end
            end
        )
-- Sliced off 6 lines

next, make it so that when you click, shotFired becomes true. do this by placing it inside the Activated event. also make sure it's inside the if not debounce statement, or else it could turn true without you firing a shot.

-- Sliced off 5 lines
        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()
    local shotFired = false

        script.Parent.Activated:Connect(
            function()
                if not debounce then
                    debounce = true
            shotFired = true -- make it so that the variable changes on click

                    script.RemoteEvent:FireServer(mouse.Hit)
                    local hum = player.Character.Humanoid

                    for i = 1, 15 do
                        wait()
                        hum.CameraOffset = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))
                    end
                    hum.CameraOffset = Vector3.new(0, 0, 0)

                    wait(2)
                    debounce = false
                end
            end
        )
-- Sliced off 6 lines

next, add a check to see if a shot has been fired. if not, we can let the player shoot, but if a shot has been fired, we have to prevent the player from shooting.

-- Sliced off 5 lines
        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()
    local shotFired = false

        script.Parent.Activated:Connect(
            function()
                if not debounce and not shotFired then -- add a check to make sure the shot has not been fired
                    debounce = true
            shotFired = true

                    script.RemoteEvent:FireServer(mouse.Hit)
                    local hum = player.Character.Humanoid

                    for i = 1, 15 do
                        wait()
                        hum.CameraOffset = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))
                    end
                    hum.CameraOffset = Vector3.new(0, 0, 0)

                    wait(2)
                    debounce = false
                end
            end
        )
-- Sliced off 6 lines

final code:

function onKeyPress(actionName, userInputState, inputObject)
    local debounce = false
    if userInputState == Enum.UserInputState.Begin then
        print("X was pressed")
        local debounce = false

        local player = game.Players.LocalPlayer
        local mouse = player:GetMouse()
    local shotFired = false

        script.Parent.Activated:Connect(
            function()
                if not debounce and not shotFired then
                    debounce = true
            shotFired = true

                    script.RemoteEvent:FireServer(mouse.Hit)
                    local hum = player.Character.Humanoid

                    for i = 1, 15 do
                        wait()
                        hum.CameraOffset = Vector3.new(math.random(-1, 1), math.random(-1, 1), math.random(-1, 1))
                    end
                    hum.CameraOffset = Vector3.new(0, 0, 0)

                    wait(2)
                    debounce = false
                end
            end
        )
    end
end

game.ContextActionService:BindAction("keyPressSpecialName", onKeyPress, true, Enum.KeyCode.X)
game.ContextActionService:SetPosition("keyPressSpecialName", UDim2.new(.5, 0, -.5, 0))
game.ContextActionService:SetImage("keyPressSpecialName", "rbxassetid://73737626")
0
what is the, {"mode":"full","isActive":false} for, its giving a a syntax error. OFF_S4LE 127 — 3y
0
what is the, {"mode":"full","isActive":false} for, its giving a a syntax error. OFF_S4LE 127 — 3y
0
what is the, {"mode":"full","isActive":false} for, its giving a a syntax error. OFF_S4LE 127 — 3y
0
it's a mistake lo User#30567 0 — 3y
Ad

Answer this question