How do I detect if the player presses an input the second time?

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!
  • I am making my own pause system as my game will be single player, and I got the scripts down already so all I need to do is make a local script that detects whenever the player presses T (T’s a placeholder for now). Pressing T for the first time will call the event to pause the game and pressing T for the second time un-pauses it.
  1. What is the issue? Include screenshots / videos if possible!
  • Whenever the player presses T, it immediately fires the event to unpause the game after the debounce timer (I added a debounce so that the player can’t spam the pause button over and over)
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?
  • I assume this is because the input is still being read by the game somehow? The most I tried to do is adding a “Paused” boolValue to the player to check if they pressed the pause button but that didn’t seem to work.
local uis = game:GetService("UserInputService")
local db = false
local dbTimer = 1
local event = game.ReplicatedStorage:WaitForChild("StopTimeEvent")
local event2 = game.ReplicatedStorage:WaitForChild('StartTimeEvent')
local plr = game.Players.LocalPlayer

uis.InputBegan:Connect(function(inp, gpe)
	if gpe then return end
	
	if inp.KeyCode == Enum.KeyCode.T and plr:WaitForChild("Paused").Value == false then
		if db then return end
		db = true
		print("player paused")
		--event:FireServer()
		plr.Paused.Value = true
	end
	
	task.wait(dbTimer)
	
	if inp.KeyCode == Enum.KeyCode.T and plr:WaitForChild("Paused").Value == true then
		db = false
		print("player unpaused")
		--event2:FireServer()
		plr.Paused.Value = false
	end
end)

I apologize if I did anything wrong regarding the creation of this post.

A lot of everything is a little wrong but you got the jist of it.

local uis = game:GetService("UserInputService")
local db = false
local dbTimer = 1
local event = game.ReplicatedStorage:WaitForChild("StopTimeEvent")
local event2 = game.ReplicatedStorage:WaitForChild('StartTimeEvent')
local plr = game.Players.LocalPlayer

uis.InputBegan:Connect(function(inp, gpe)
	if gpe then return end

	if inp.KeyCode == Enum.KeyCode.T then -- You cannot have two of the same key checks ESPECIALLY separated by a wait in the middle of them. You're just reading the same Input object in the call. It will always be true in both cases for your old code.
		if db then return end
		db = true
		print(`player {if plr.Paused.Value then "unpaused" else "paused"}`) -- if the value is currently true we're setting it to false so unpause and likewise false is pausing
		--event:FireServer()
		plr.Paused.Value = not plr.Paused.Value -- Don't use constants for switching boolean values when they can be replaced with a not operator
		
		task.delay(dbTimer, function() db = false end) -- Only delay if the condition is true. Otherwise, you can't type or press other buttons without it setting the debounce and then waiting to unset it again.
	end

end)

Thank you I appreciate it, but now what can I do to fire the second event which un-pauses the player?

Make it one event and pass the value of the Paused value after this line:

plr.Paused.Value = not plr.Paused.Value

Like so:

event:FireServer(plr.Paused.Value)

Server side:

event.OnServerEvent:Connect(function(ClientThatSent, State)
	if ClientThatSent then -- There was no reason to check for the type so instead I'm just editing this to check for the Player, which is not necessary at all, but shouldn't add too much bulk as you're not spamming this remote.
		State = not not State -- Forces a type change ensuring no odd behaviors, any truthy value is still true and any falsy value is still false.
		-- The type change is not even necessary but if you need to, you can use this value as a boolean now for any operations you may do for the following code.
		if State then
			-- do your pause code (the Client requested a pause)
		else
			-- do your unpause code (the Client request to unpause)
		end
	end
end)

Okay this works, thanks for the feedback

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.