How to detect stuns in speed anti cheat

Hello! I’m have problems with detecting slows or stuns in my anti cheat so the player doesn’t move while they’re supposed to stand still. The below code works fine for what it does now which is make sure the player doesn’t go too fast but because of how long it takes for players to come to a complete stop by setting their walkspeed to 0, I’m having difficulties making sure they don’t move when they’re not supposed to while not sending too many false positives.

Does anyone have a solution for this?

local oldPos
local maxDistBase = 4
local maxDistAllowed = 4

while true do
    if oldPos == nil then
        oldPos = Vector3.new(Root.Position.X, 0, Root.Position.Z)
    end
    
    local currentPos = Vector3.new(Root.Position.X, 0, Root.Position.Z)
    local distRan = (oldPos - currentPos).Magnitude
    maxDistAllowed = maxDistBase
    
    for _,v in ipairs(CharFolder:GetChildren()) do
        if v.Name == "SpeedAdd" then
            local addValue = v.Value * .15
            
            maxDistAllowed = maxDistBase + addValue
        end
    end
    
    if distRan > maxDistAllowed then
        print(distRan)
        RunOccurenceCounter()
    end
    
    oldPos = currentPos
    task.wait(.1)
end

local occurence = 0

function RunOccurenceCounter()
    occurence += 1
    
    if occurence > 5 then
        --Kick the player for assumed cheating
        print("Player is assumed cheating")
    end
end

while true do
    if occurence > 0 then
        occurence -= 1
    end
    task.wait(8)
end

You might add a tag somewhere that a player is stunned and use a different check then.

@esadams188 You are not even trying to hide the fact that your comment is written by an AI…

Did you generate this comment with GPT3?

This feels like the most AI generated content i’ve seen, all three of these points do not answer the question at hand.


To detect player movement when their “anchored” or “stunned”, I would personally just track the distance from when the player is stunned to when they are unstunned, if the magnitude extends 1 or so then i’d invoke an action on that player.

if a player is moving a certain distance within a certain time frame, and if they exceed that distance, it increments a counter. If the counter exceeds a certain threshold, the player is assumed to be cheating and is kicked.

To detect if a player is stunned, you could modify this code to check for a stun condition instead of a distance threshold. Here is an example of how you could do this

-- Global variables
local oldPos
local maxDistBase = 4
local maxDistAllowed = 4
local stunThreshold = 5 -- number of stuns needed to kick player
local stunCounter = 0 -- number of stuns player has received

-- Main loop
while true do
    if oldPos == nil then
        oldPos = Vector3.new(Root.Position.X, 0, Root.Position.Z)
    end
    
    local currentPos = Vector3.new(Root.Position.X, 0, Root.Position.Z)
    local distRan = (oldPos - currentPos).Magnitude
    maxDistAllowed = maxDistBase
    
    -- Check for stun condition
    if IsPlayerStunned() then
        -- Increment stun counter
        stunCounter += 1
        
        if stunCounter >= stunThreshold then
            -- Kick player for cheating
            print("Player is assumed cheating")
        end
    end
    
    oldPos = currentPos
    task.wait(.1)
end

-- Function to check if player is stunned
function IsPlayerStunned()
    -- Replace this code with your own logic to check if the player is stunned
    return math.random() < 0.1
end

This modified code will check if the player is stunned every 0.1 seconds, and if they are, it will increment a counter. If the counter exceeds the stunThreshold , the player is assumed to be cheating and is kicked. You will need to replace the IsPlayerStunned function with your own code to check if the player is stunned.

I think what you want is to check if the player is in a “knock back” state. To do this, you can use the Humanoid/GetState() function, which returns a value that you can use to check if the player is in a “knock back” state.
Here is a link to the documentation for the Humanoid/GetState() function:
https://developer.roblox.com/en-us/api-reference/function/Humanoid/GetState