Need help understanding this code

I’m trying to code sanity checks into my project and I need help understanding this code

So the code is from this thread How do i make a sanity check for Remote Events / Functions - #5 by goldenstein64

I understand most of this but the function inside delay is confusing me

local playerDebounces = {}

Players.PlayerAdded:Connect(function(player)
	playerDebounces[player.UserId] = false
end)

Players.PlayerRemoving:Connect(function(player)
	playerDebounces[player.UserId] = nil
end)

Remote.OnServerEvent:Connect(function(player,...)
	local id = player.UserId
	if playerDebounces[id] == false then
		playerDebounces[id] = true
		delay(5, function() playerDebounces[id] = playerDebounces[id] ~= nil and false end) -- I don't understand here, can someone please explain this part to me 
		
	end
end)



Basically that’s just the weird way to say

if playerDebounces[id] ~= nil then 
 playerDebounced[id] = false 
end
1 Like

Thanks I understand now, but is their more information on this way of writing code

Yes. Conditional Statements 101 (+Luau Ternary)

tbh though I still don’t even understand it so I’ll just stick to writing conditional statements the ol’ fashioned way

1 Like

Those are basically Lua’s version of ternary operators. Typically, languages have something like this:

const item = condition ? item1 : item2

This is basically setting item to item if condition is true. If condition isn’t true, it sets item to item2 instead. Lua has something like this:

local item = condition and item1 or item2

In most cases, this functions the same as ternary operators in other languages (ES6 javascript in the first example)

1 Like