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")
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")