Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

How can I use tick() to figure out when a player last fired a remote event?

I’m thinking of many ways to stop exploiters from cheating in games where accurate raycasting is needed from a gun. I figured that someone exploiters may try to fire a shooting event many times within a close space. I’ve never used tick() before but I heard that it returns the players local time down to the millisecond, how could I use this along with a defined fire rate of a gun, detect if the player is shooting quicker than the actual fire rate?

1 comment

0

Marmalados, I suggest being careful. You're asking opinion-based questions and they might start getting moderation actions onto them.

1 answer

✓ Accepted answer

#####Please accept this answer if it helped.

##Question

How can I use tick() to figure out when a player last fired a remote event?

##Answer When the player fires a remote, we can set their index in a dictionary to a timestamp of when they last fired the event.

How could we set up our dictionary? Well when a player joins we can add them to the dictionary, set their timestamp to 0 since they never initially fired the remote, and when they leave, set their value to nil in the dictionary. We can compare the current time stamp and then get the difference from the previous one and compare if its within a range to see if they are properly firing in the right time.

###Side Note: What is tick? tick is a function and when called returns the amount of seconds since epoch (unix). tick is very similar to os.time but returns also the fraction of a second since epoch. Vanilla lua does not have a tick, this is specific to roblox. More informaton about os.time is listed here: PiL 22.1

###Side Note: Latency

how could I use this along with a defined fire rate of a gun, detect if the player is shooting quicker than the actual fire rate? Be careful here. Make sure to allow a margin of error for latency in the logic of the code since a player could be lagging.

###Case example applied in code

--// server
local players = game:GetService("Players")

local event = ... --// reference to remote event
local eventDebounce = 1 --// how many seconds before the client should be actually firing the event again. 

local timeStampDictionary = {} --// where we will store player timestamps

local function addToTimeStampDictionary(player)
	timeStampDictionary[player] = 0 --// add them to the timestamp dictionary with a timestamp of `0` since they never really fired the event in the first place. 
end

local function removeFromTimeStampDictionary(player)
	timeStampDictionary[player] = nil --// set their index to nil since they left and we don't need it anymore. 
end

local function onServerEvent(player, ...)
	local currentTimeStamp = tick()
	local lastTimeStamp = timeStampDictionary[player]

	if (currentTimeStamp - lastTimeStamp) > eventDebounce) then --// are they firing the event again and its been more than `1` second since we last fired. (`1` second because `evnetDebounce` is set to `1`)*
		print("success!")
		timeStampDictionary[player] = currentTimeStamp --// set new stamp because successful. 
	else
		print("oops! they fired to early")
	end
end

players.PlayerAdded:Connect(addToTimeStampDictionary)
players.PlayerRemoving:Conect(removeFromTimeStampDictionary)

event.OnServerEvent:Connect(onServerEvent)

This discussion is preserved as it was. New answers, comments, and votes are closed.