First simple anti exploit system

Hello developers, I made a simple SERVER SIDED anti exploit system that is compatible with HD Admin, any features I should change? (Yes, I am cleaning up the “if HDRank == Admin or etc.”)

local hd = hdMain:GetModule("API")

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character
    
    player.CharacterAdded:Connect(function(character)
        local hum = character.Humanoid
        local character = player.Character
        local rootPart = character.HumanoidRootPart
        
        
        while true do
            local Pos1 = rootPart.Position
            wait(0.1)
            local Pos2 = rootPart.Position 
            local difference = ((Pos1 - Pos2) * Vector3.new(1, 0, 1)).Magnitude
            
            local rankId = hd:GetRank(player)
            local HDRank = hd:GetRankName(rankId)
            local isAdmin = false
            
            if HDRank == "Mod" or HDRank == "Admin" or HDRank == "HeadAdmin" or HDRank == "Owner" then
                isAdmin = true
                -- Loops again
            elseif difference > 5 then
                rootPart.Position = Pos1
            end
        end
    end)
end)```
3 Likes

Have you tested your code? This code looks fine but you should test it for false positives.

For the admin stuff what I would do is put it outside the loop, that way it doesn’t keep on checking.

game.Players.PlayerAdded:Connect(function(player)
    local character = player.Character
    
    player.CharacterAdded:Connect(function(character)
        local hum = character.Humanoid
        local character = player.Character
        local rootPart = character.HumanoidRootPart
        
         if HDRank == "Mod" or HDRank == "Admin" or HDRank == "HeadAdmin" or HDRank == "Owner" then
              print('mod+')
         else
              while true do 
                   -- code continues

Anyways, the only thing you need to be aware of is the fact that there may be false positives. What if someone like an admin did :speed all 50 for a hide and seek event or something (who knows why)

What I would do is use the player’s walkspeed to determine the distance (good post here) regarding studs per second.

Secondly, I would take lag somewhat into consideration, for instance if someone stays still for a few seconds, they may be laggy and then “teleport” a couple of seconds later.

You could have like a maxDistance variable, and then add to that variable if the person stays still :stuck_out_tongue:


Other than that

local difference = ((Pos1 - Pos2) * Vector3.new(1, 0, 1)).Magnitude

This is a pretty smart idea! :clap: (might use that myself)

2 Likes

Thanks for the help, feel free to try that

This is a great system, just you have to make sure you test this a lot. You do not want people think they are lagging by being tped back to their last position. You just need to do lots of testing for your own game.

Thanks, I will be testing it a lot

A general rule of thumb you can almost always apply to get great performance boosts in scripts is that you shouldn’t call .new or :Function() (ex. :GetDescendants()) because these types of code are usually always slow.

Instead of calling Vector3.new(1, 0, 1) every 0.1 seconds, make it a variable at the top of the script. The performance boost it gives is more than negligible. Just for researching sake, I found that it was more than 10x faster as storing the Vector as a local variable.

You also don’t need to make local Pos2 a variable at all, it isn’t reused anywhere in the script.

I won’t get into how bad the HDRank == part is, because you’ve stated you’re going to clean it. I suggest creating a cache of UserIds that are admins. For example, when someone joins, check once if they’re an Admin, if they are, add their UserId to the cache. I’m not sure how optimized HD Admin’s :GetRank() is, so creating a cache would gaurantee better performance.

You also don’t need the local character variable at the top of the script.

2 Likes

Thank you! I will take that into mind, and go try that

Do you mean a var for looping or this

local Vec = Vector3.new(1, 0, 1)
		
		while true do
			local Pos1 = rootPart.Position
			wait(0.1)
			local Pos2 = rootPart.Position 
			local difference = ((Pos1 - Pos2) * Vec).Magnitude

What? These won’t result in “great” performance boosts, but negligible + considering the fact that these functions have been optimised in the new Luau VM anyways.

Please don’t provide false information and false sources, as you would end up manipulating people into believing you are correct when in fact you are incredibly wrong. That will only result in a negligible boost, even less than it. Vector 3 creation is very optimal also considering the fact that Roblox has optimised them even more.

Instead of researching, at least try to test.

I understand my example was poor, that’s my fault, but I’m still correct. I pointed out how the function :GetRank() wasn’t in the post so I had no idea of how optimized it is, them calling it once and then doing a loop is way better than them doing it every loop.

My example of :GetDescendants() should’ve been replaced with something much more resource using, like :GetCharacterAppearanceAsync().

local HolderVariable
local Players = game:GetService("Players")
local TimeStamp = tick()
for Count = 1, 10 do
	HolderVariable = Players:GetCharacterAppearanceAsync(1)
end
print(tick()-TimeStamp) -- Over one second.

Feel free to test this out yourself. Doing it once and then looping is much more resourceful and has much better performance than doing it in a loop. Might I add the loop ran 10 times, one second for 10 loops is horrible.

Now that’s just a rule of thumb, there are times where it is negligible, but again, I didn’t know how optimized (if at all) :GetRank() was. I apologize though that my example was bad.

local HolderVariable
local TimeStamp = tick()
for Count = 1, 1000000 do
	HolderVariable = Vector3.new(1,1,1)
end
print(tick()-TimeStamp) -- 0.03 seconds
local HolderVariable
local HolderVector = Vector3.new(1,1,1)
local TimeStamp = tick()
for Count = 1, 1000000 do
	HolderVariable = HolderVector
end
print(tick()-TimeStamp) -- 0.003 seconds

I understand how it sounded when I said researching, but I did do testing while writing out my reply. It’s evident that it’s atleast 10x faster.

OP’s script was creating Vector3’s in an unoptimized way rapidly, whereas my solution would help with performance. I would argue that it’d be close to negligible, it’d still be good to do though, just look at the numbers if you think otherwise.

1 Like

I understand my example was poor, that’s my fault, but I’m still correct. I pointed out how the function :GetRank() wasn’t in the post so I had no idea of how optimized it is, them calling it once and then doing a loop is way better than them doing it every loop.

No you aren’t. That will make a negligible difference.

I understand how it sounded when I said researching, but I did do testing while writing out my reply. It’s evident that it’s atleast 10x faster.

No it clearly isn’t 10x faster. You’re creating 1 million vector’s yet it still takes 0.03 seconds, which clearly proves my point. Also consider the fact that Roblox released their own faster Vector3’s which would be even more faster, and OP doesn’t literally create a million vectors.

The difference would only be significant if it took around 0.3 seconds approximately.

OP’s script was creating Vector3’s in an unoptimized way rapidly, whereas my solution would help with performance. I would argue that it’d be close to negligible, it’d still be good to do though, just look at the numbers if you think otherwise.

No it wouldn’t. Your statement indicates that you don’t know how memory works. OP is creating Vector3.new(1, 0, 1) but it has no references. Meaning after the next iteration when its set to nil automatically, it is garbage collected from the memory when the garbage collector runs. You are clearly on micro optimization which is heavily bad practice.

The speed at which OP’s code runs is * completely * fine. You should look into cleaning up rather than caring about small speed boosts. You should only look to improve speed where it * matters * and is necessary, it doesn’t matter here and the difference is also negligible.

PS: Do you also realize that the new Luau VM is released? Meaning global and other functions are around 20-40% more optimised. GetDescendant will get expensive based on how many descendants are there, so your point still isn’t valid.

1 Like

This script is gonna be a nightmare for a laggy player

Any ideas of fixing it a bit more?