Anti-fly script

So I tried to script an anti-fly script. I want some advice, how can I improve this?

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(character)
		while task.wait(0.1) do
			local HumanoidRootPart = character:WaitForChild("HumanoidRootPart")
			local Humanoid = character:WaitForChild("Humanoid")
			local Result = workspace:Raycast(HumanoidRootPart.Position, HumanoidRootPart.Position + Vector3.new(0, -200, 0))
			if Result then
				local Distance = Result.Distance
				if Result.Distance > 25 and Humanoid:GetState() ~= Enum.HumanoidStateType.Freefall
				and Humanoid:GetState() ~= Enum.HumanoidStateType.Running
				and Humanoid:GetState() ~= Enum.HumanoidStateType.Jumping
				and Humanoid:GetState() ~= Enum.HumanoidStateType.Landed then
					print(Humanoid:GetState())
					game.Players:GetPlayerFromCharacter(character):Kick(
						"Kicked for fly cheats. If you think this was a mistake, please contact the owner of the game."
					)
				end
			end
		end
	end)
end)
2 Likes

It should average over a long period of time to prevent accidental kicks due to lag spikes. Also someone can avoid detection by making sure their character is in one of the states you’ve made an exception for such as freefall.

Btw, humanoid states come from the client (I’m 99% sure). Instead, use Humanoid.FloorMaterial. I am making an anti-cheat open sourced around Christmas time, so stay tuned for that. My anti-cheat isn’t some scummy one either, it’s the best 200 lines of code of your life.

1 Like

But what about when the player falls off something? Like an obby? It would kick the player

1 Like

Maybe just do a full anti cheat system, so they just get kicked for injecting any type of software.

1 Like

Bro what how do I detect when they inject something

That’s actually impossible. Even if someone wants to inject something, they’re going to delete your local script.

Look up HowToRoblox Anti-Cheat in YouTube and play with that a little. Make sure to delete the part where it handles no clip too.

Put this script inside of ‘StarterPlayerScripts’ in a Local script.

local players = game.Players.LocalPlayer
local char = false

players.CharacterAdded:Connect(function(character)

	char = character
end)

while wait() do

    if char == false then
    	continue
    end
	
	local Humanoid = players.Character:FindFirstChild("Humanoid")

	if Humanoid == nil then
		continue
	end

    local gotState = Humanoid:GetState()

	if gotState == Enum.HumanoidStateType.PlatformStanding then
		players:Kick("Hacking")
	end
end
2 Likes