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)
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.
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