Here is a forum link I fell on while I had the same question as you. It’s not UserInputService but still you can implement it.
local last = 0
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
local now = tick()
if not last or (now - tick()) > 2 then
last = tick()
elseif (now - last) <= 2 then
last = nil
print("do stuff")
end
end
end)
I tried to make something. But nothing happened. I tried to print something. It said: attempt to perform arithmetic (sub) on number and function
EDIT: I put script in starterCharacter.Should I change it?
EDIT2:(i cant post anymore so i did this.) Still Same
It worked but i tried to put some variables but variables didnt exist.
where i put variable:
local last
game:GetService(“UserInputService”).InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
local now = tick()
if not last or (now - last) > 2 then
last = tick()
elseif (now - last) <= 2 then
last = nil
You technically have defined the variable something, just that you haven’t set it to anything so it defaults to nil. You aren’t doing anything with it though, and the red line is there over the word something beneath where the variable is defined because you need to write an expression. If you wrote something like something = true there, the red line wouldn’t be there. What are you using this for anyways?
You have been given a script to detect if there’s two key presses within a certain time period
What do you mean by this? Are you not setting its parent when you create it?
You can try something like this:
local last = 0
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local hrp = character:WaitForChild("HumanoidRootPart")
game:GetService("UserInputService").InputBegan:Connect(function(input)
if input.KeyCode == Enum.KeyCode.W then
local now = tick()
if not last or (now - last) > 2 then
last = tick()
elseif (now - last) <= 2 then
last = nil
local bv = Instance.new("BodyVelocity")
bv.Velocity = hrp.CFrame.lookVector * 50
bv.Parent = hrp
wait(0.2)
bv:Destroy()
end
end
end)
I assume you’re doing something along the lines of this:
local something
something -- you just put something here
As stated in the script analysis, the compiler expects an assignment or a function call, but you’re doing nothing with it and just leave it there. You’ll have to do something like:
-- this is just an example
local something
something = true -- no errors
or
-- this is also an example
local something = Instance.new("BodyVelocity")
something.Parent = workspace:FindFirstChildOfClass("Part") -- no errors either
--Variables
--Set the times clicked to 0 before the script
local TimesClicked = 0
--Set a nil variable that we are going to use right after
local theFirstTime
--Function
local function MyInputEvent(input, gameProccess)
--Check if there arent any proccess happening
if gameProccess then return end
if input.KeyCode == Enum.KeyCode.W then
--Check the ammount of times that was clicked and increase the clicked
TimesClicked = TimesClicked + 1
if TimesClicked == 2 then
--Set back to 0
TimesClicked = 0
local theMoment = tick()
--Get the current time and then subtract with the old time
if theMoment - TheFirstTime <= 0.8 then
-- Compare them
--The code you want
end
elseif TimesClicked == 1 then
--Give the time the first click happened to the nil value that was set up in the start
TheFirstTime = tick()
end
end
--Event
game:GetService("UserInputService").InputEnded:Connect(MyInputEvent)
-- Using input ended in case I would only want the code to run when i finish clicking the mouse