Hello! I have an ability that can be charged to become more powerful. The longer the player holds down a key, the more powerful the ability becomes. The client would have to fire an event to the server to indicate when it has held and let go of the key. I thought of allowing the server to calculate the difference between the time it recieved the 2 events. But if a player lag spikes while holding a key, the key held and released events might be fired at about the same time, even if the player held it down for a while. How would I make it more accurate?
make client send data for how long it held?
Make sure there it gets snapped to server’s calculation if differance is too big.
You could calculate the time between the 2 inputs solely on the client, using something like tick() or os.clock(), and then send that to the server. I just thought of this because firing 2 events for a single attack/ability seems like a lot, especially when there is a lot of players.
I guess just calculate the time on the client, send it to the server, and make the server check if the time is valid.
How do you check if it is valid on the server if you only send the calculated time on the client? The server doesn’t know when you started, so it’s a bit difficult to validate
You check if the time is too short or too long, and look out for any anomalies that might be sent through as well. You only really need to validate if the number is too big, small or some other criteria you may have. But calculate the difference on the client. You can also send in the time when the attack started and the attack finished, and validate the difference and both the end and start on the server, but it seems quite redundant.
But the main idea is, make the client calculate the difference, send it to the server, and make the server check if the difference is valid and can proceeed with the next part of your code/attack.
Don’t let the client be the SSOT for the calculation. That remote could be exploited very easily.
Your best option is to use opportunistic updates for user input. You can’t rely on the client at all, but you can show the update on the client in question. The other players in the server wouldn’t know a difference.
Fire a remote event on input end and begin. This should contain a unix timestamp to also get network delay. Deal with replication on other clients separate from input client. If they lag they know they are lagging but the ability would still work. Once they stop lagging, we get event on server and do some validation with the timestamp and release time.
You can also tell very easily if they manipulated the request timestamp. You can determine this by getting the delta between the start and end input and the total time between requests. If they are not within a certain range it’s obviously not right. Then we reject the call before anything else.
what i did i:
UserInputService.InputBegan:Connect(function(input)
if input.Keycode.Name == "SpaceBar" then
StartTime = tick()
--do the charge animation or whatever you need visually
end
end
UserInputService.InputEnded:Connect(function(input)
if input.Keycode.Name == "SpaceBar" then
EndTime = tick()
end
end
what is does is simply storing the two times, this should be the most optimized way and achieves the same result (i wrote this without studio, so it could have errors, buts the point is that)
side note, i use this method in my game to charge a jump and after relasing it i do something like:
local FinalResult = math.clamp(EndTime - StartTime, 0, 3) --Max Seconds to hold: 3 seconds
local power = JumpPower * (20 * FinalResult)
as this is the simpliest one, but there are other good methods if you need
err thats a lot of info for my small brain to take in, is it ok if you simplify it a bit sorry ![]()
so if i understand correctly, when the server recieves an event of the input begin, it also recieves something that tells the server the delay, and the server can get the time that the input begin and ended more accurately
what is unix timestamp, and how would the server get that info? and what is the range that make it obviously not right? if the player…somehow held and released the input while having one large lag spike throughout, wouldn’t the server recieve the 2 events almost immediately? then validating becomes difficult