Server-Sided Anti Infinite Jump

I’m preparing to add this code I’ve developed in a separate game to my game, and I want to make sure that I’m not forgetting to account for anything before I add it. Any feedback is welcome.

local Players = game:GetService("Players")
local LastJumps = {}
local Connections = {}
local MinJumpDelay = 0.25

Players.PlayerAdded:Connect(function(Player)
	local Id = Player.UserId
	Connections[Player.UserId] = Player.CharacterAdded:Connect(function(Char)
		LastJumps[Id] = 0
		local Humanoid = Char:WaitForChild("Humanoid", 2)
		if Humanoid then
			local StateConnection
			local DiedConnection
			local function OnStateChange(State)
				if State == Enum.HumanoidStateType.Jumping then
					print(tick() - LastJumps[Id])
					print(Humanoid.FloorMaterial)
					if LastJumps[Id] and ((tick() - LastJumps[Id]) < MinJumpDelay) and Humanoid.FloorMaterial == Enum.Material.Air then
						Player:Kick("InfJump")
						StateConnection:Disconnect()
						DiedConnection:Disconnect()
					end
					LastJumps[Id] = tick()
				end
				if State == Enum.HumanoidStateType.Landed or State == Enum.HumanoidStateType.Running or State == Enum.HumanoidStateType.RunningNoPhysics or State == Enum.HumanoidStateType.Climbing then
					if LastJumps[Id] then
						LastJumps[Id] = tick() - MinJumpDelay
					end
				end
			end
			StateConnection = Humanoid.StateChanged:Connect(OnStateChange)
			local function OnDied()
				StateConnection:Disconnect()
				DiedConnection:Disconnect()
			end
			DiedConnection = Humanoid.Died:Connect(OnDied)
		end
	end)
end)

Players.PlayerRemoving:Connect(function(Player)
	Connections[Player.UserId]:Disconnect()
end)

Thank you!

6 Likes

Whenever a player leaves, their character is destroyed which means that all connections bound to the player’s character are disconnected.

No need to disconnect the connection when they leave, it’s useless.

4 Likes

You don’t need to do Connections[Player.UserId], you can just use Connections[Player].

1 Like

Very late on this topic but it seems to me that your anti inf jump is flawed. You’re keeping track of the last time the player has jumped then comparing it to see if they have jumped more than they should.

This is flawed because the exploiter can jump at irregular intervals to bypass this. However, they wouldn’t be able to jump up, but only descend.

1 Like