Tick() being weird

Alright so I’m endeavoring to detect the duration a player holds the letter E, and it’s working I conjecture, but it sometimes throws an error and it breaks the whole event I have it in which is a bindable event.

I pass timepass from a local script with inputbegan, and then on inputended I pass over timepass as an argument with a RemoteEvent I fire to the server, and I use a bindable event and fire timepass through that then I have a server script that picks it up which is the one below



Line 79-83 :

Thwip.Event:Connect(function(timepass)
	if tick() - timepass <= .5 then
		print("Thwip!")
	end
 end)	

timepass must be nil then. tick() should always return a value.

Well I pass timepass from a local script with inputbegan, and then on inputended I pass over timepass as an argument with a RemoteEvent I fire to the server, and I use a bindable event and fire timepass through that then I have a server script that picks it up which is the one above

You dont really need the event. (Unless you are trying to use filtering enabled)
You just would need to create a variable at the start of the code and have its value to nil and then set it to tick() in the InputBegan. In the inputended you do timePass = tick() - timePass. There you will already know the difference between time

So all you will do will be timePass <= 0.5. I will show a example

local timePass
local UIS = game:GetService("UserInputService")
UIS.InputBegan:Connect(function(input, gameProccess)
       if gameProccess then return end
       if input.KeyCode == Enum.KeyCode.E then
          timePass = tick()
       end
end)
UIS.InputEnded:Connect(function(input, gameProccess)
       if gameProccess then return end
       if input.KeyCode == Enum.KeyCode.E then
          timePass = tick() - timePass
          if timePass <= 0.5 then
             print(timePass)
          end
       end
end)

But for your case, timePass is nil.

Yeah problem is I cant really do it on the client because I need to detect it when the player is doing something on the server

And I tried making the variable before I use it in the inputbegan and it’s still throwing an error sadly

Well you can try using a remote function, You can do the same things I said but instead you pass timePass to the server and then you return the timePass (Basically you do timePass = tick() in the server and then return it to the client, so you update timePass to the value you returned ).

In the input ended case you do the same thing and do the check in the server

I really dont want to have to use a remote function to do this because the whole point is to just send it from the client and not get info back

Is there any other way I can do this so it can stop making it nil??

Maybe sounds too simple but I’d do this.

Thwip.Event:Connect(function(timepass)
	if tick() - (timepass or tick()) <= .5 then
		print("Thwip!")
	end
 end)

It sounds almost stupid but that (timepass or tick()), if timepass is nil, it will replace it with tick()'s value instead so it doesn’t error because of an non-existing value and at least has some value to use (tick() returning a number so it can subtract without error).

I use the (MyValue or 10) a lot in my code so that if MyValue somehow ends up being nil, it always gets replaced by an placeholder value just so it doesn’t error.

Though of course, you should look at why timepass is nil, because I think you are trying to subtract nil from a number value, which shouldn’t be possible and thus errors.
Take a look at all those scripts who make use of this event and what values are being passed as params/args and check what happens with those values and why they could possibly be nil, or something that isn’t a number.

You don’t want to mix tick() between server and client in the case that the server’s machine is running on a different timezone then the client.
If it must be done on server, I suggest sending a remote event to server when client begins input. Keep track of that and later compare it when remote event is sent again for input ended.

But tick() will always return, so somewhere in your current model timePass is lost and is returned nil in the function you outlined.

1 Like

Alright so the problem is fixed but now the problem is if I hit one key it works, but as soon as I hit the other key it prints out both which I dont get


https://gyazo.com/71c9093cf1cb9f9cb9e4704e633f0d02

So you can try sending the timePass to the server, and in the server you could do something like this:

-- This is the server
local timePass
RemoteEvent.OnServerEvent:Connect(function(player, timepassed)
       if timePass then
             --Checks if timePass has a value
             if tick() - timePass <= 0.5 then
                   -- Checks the difference between the current time and the time of the input began
                   --Code
             end
       else
            --If timePass is nil then it means it has no value so we set it to timepassed
            -- Remember to only pass timepassed at the inputBegan
            timePass = timepassed
       end
end)

(Whoops sorry I didnt see you said the first problem was solved)

Well I dont know how to solve this but I guess its because WebE and WebQ are being fired simutaneously (Like the events are swapped.), A solution for this could be maybe switching the places of both the conditionals
(letter == 2 and letter == 1)

To inside the .Event of each one so it can try to avoid

Nope that sadly didnt work, and the problem is im basically making a web swinging system and trying to detect how long the player holds and release either q or e

Not sure what letter is supposed to represent and what reason you have for using if conditionals.

You should note that if at any point letter == 1, it will create a new event listener which will always fire when WebE is fired. If this code runs repetitively (like this is all under an event fired listener), you’re probably going to have memory leak issues spawning more and more event listeners every time.

Anyway, you’re looking in the wrong place for this [both print statements being displayed] problem. It’s going to be in the code where you fire WebQ or fire WebE. It doesn’t seem like you’re not distinguishing which is fired between Q and E inputs, so they are both fired. And since both connections exist (multiple times) both print statements are printed (multiple times).

1 Like

We can’t really help without more details. You showed us where it is printing but not where any of the variables are defined.