Server-Sided Anti Cheat Help

Ello, so recently I have decided I want to make an Anti Cheat. Anyways I have learned that Server Sided ones are much more secure and reliable. Luckily for me I researched enough to understand Client Sided Anti Cheats, not Server Sided ones. Anyways I looked on YouTube and found a tutorial for one but it is merely for speed hacks. What it does it if your walkspeed is above 21 (I set it to 21 because some games have sprint abilities etc.) and it teleports you back.

Anyways because it only supports speedhacks atm, I am seeking help to add things such as, jump power detetcion, noclip, and god mode for now. So any help would be great. Heres the code:

game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		while true do
			local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
			local CurrentXPos = HumanoidRootPart.Position.X
			local CurrentZPos = HumanoidRootPart.Position.Z
			wait(0.5)
			local NewXPos = HumanoidRootPart.Position.X
			local NewZPos = HumanoidRootPart.Position.Z
			local NewYPos = HumanoidRootPart.Position.Y
			local XDistance = NewXPos - CurrentXPos
			local ZDistance = NewZPos - CurrentZPos
			if XDistance >= 9 or XDistance <= -9 or ZDistance >= 9 or ZDistance <= -9 then
				HumanoidRootPart.Position = Vector3.new(CurrentXPos, HumanoidRootPart.Position.Y,CurrentZPos)
			end
		end
	end)
end)

I’m just asking for a simple explanation at seeking these, you guys do not have to write massive lines of code. Any help is appreciated :smile:

3 Likes

First, it needs great improvements

  1. The loop would still run after the character is dead, so
while player.Character ~= nil do
  1. Use magnitude, no need to put multiple variables of z x and y
  2. Its too fast and might lag. instead, do it every 5 seconds, and magnitude 30.

good luck

1 Like

Alright ill change the while true do to youe code snippet. Also wdym magnitude?
Also ill add a wait for 5 second then a repeat thing.

1 Like
(Vector3.new(oldpos) - Vector3.new(currpos)).Magnitude
1 Like

Hey, I don’t really understand this can you write a simple explanation?

Magnitude is the distance between two vectors, or in your case. The distance between when the character’s position was checked before and the character’s position after waiting 0.5 seconds.
With this you can measure how fast the player is walking/flying/any way of moving from point A to point B, and you can calculate their walkspeed from there

2 Likes

No it is not laggy. Although there can be improvements, this is far from laggy.

If you have an interval of 5 seconds, the player may die then respawns within that interval, thus the code still runs. It’s better if you just checked the Humanoid.Health instead

Vector.Magnitude is a property that measures the scalar distance of the vector.
Examples:

Vector3.new(4,0,0).Magnitude -- 4
Vector3.new(0,3,4).Magnitude -- 5
Vector3.new(7,0,3).Magnitude -- 7.61577

The third example may be weird, but that is actually the distance of 7, 0, 3

Also, here is a better copy of your code.

local XZVector = Vector3.new(1,0,1)
game.Players.PlayerAdded:Connect(function(player)
	player.CharacterAdded:Connect(function(char)
		local HumanoidRootPart = char:WaitForChild("HumanoidRootPart")
		local Humanoid = char:WaitForChild("Humanoid")

		local OldPos = HumanoidRootPart.Position
		while Humanoid.Health > 0 do
			wait(0.5)
			local CurrentPos = HumanoidRootPart.Position
			local Distance = (CurrentPos - OldPos)*XZVector
			if Distance.Magnitude > 9 then
				HumanoidRootPart.Position = OldPos
			end
			OldPos = CurrentPos
		end
	end)
end)

You may see that I have XZVector in there. This is used to ignore the vertical distance.

I have a question, is 9 half the walkspeed of the player? I’m talking about these

Ah smart, then the script is not running if your dead. Also factored in the magnitude tysm! How would I add different features to it though?

For your question, I don’t know. In the tutorial it was 10 which made the max walkspeed you had 32 before the AntiCheat kicked in, I changed it to 9 which made the max walkspeed you can have 21.

1 Like

You mean these things?

Jump power detection depends on your game. If you can fly in-game, then it reduces potential. If there are extreme knockbacks, or stuff that fling you upwards, there may be false positives.

Noclip detection is not really that simple. There are many things to check for noclip.

God mode? What exactly do you mean?

Like everything else, the speedhack detection has its flaws too. If the player gets flung like a hundred studs per second, the speedhack says that the player is moving too fast. This speedhack is more like a speed cap.

God mode as in your health being over 100.

1 Like

Oh, it is actually impossible to go beyond your maxhealth. Even if the exploiter changed their maxhealth, it only applies for the client.

2 Likes

kinda wrong, they have some funny method to go god mode without even messing these properties, they parent the humanoid to nil and make some mover script for the character. it works

1 Like

Alright so then with that being said, we just have to check for every change of the Humanoid’s Health.

Humanoid.HealthChanged:Connect(function(newHealth)
	if newHealth > 1000 or Humanoid.MaxHealth > 1000 then
	
		print("They are godding...")
		Humanoid:ChangeState(Enum.HumanoidStateType.Dead)
	end
end)

To prevent anyone from god-modding… This may not be as accurate as it’s still prone to false positives, but this would mainly go over everyone’s expectations.

1 Like

To counter god-mode it is kind of what @commitblue said e.g

they parent the humanoid to nil

They delete the server-sided Humanoid and create their own local one that they have control over but to counter this you can use the .ChildRemoved event e.g

Player.PlayerAdded:Connect(function(plr)
    plr.CharacterAdded:Connect(function(character)
        character.ChildRemoved:Connect(function(v)
            if v:IsA('Humanoid') then plr:Kick('god mode') end
        end)
    end)
end)

Edit: this does work and it has been tested

To counter walkspeed events I recommend using a ‘RunService’ event and comparing their current position with their last e.g (Vector3.new(oldpos) - Vector3.new(currpos)).Magnitude

and then check what if they are moving x studs in a second and if it is abnormal you could give them three false positives and place them back into their original positon but if they continue speeding then kick them

1 Like

I believe this dosent work if your just using parent nil. Maybe check for parent too

1 Like

There is no need to check for the parent because ChildRemoved is checking what child has been taken out of (if an instance in the character has been parented to something else); so if the exploiter deletes their server-sided Humanoid the changes would replicate to the server (Humanoid would be removed and the server would detect this change) and they make their own local one which the server cannot see - so this is a simple solution to countering this specific problem.

1 Like

You could have a script (assuming that is that script that would handle anti-cheat), to clone all possible parts within your map and size them like a stud smaller keeping it’s position, making them uncollidable and adding a touch event to detect any possible no-clipping. It can be easily created by just doing this:

for partsInWorkSpace, part in ipairs(game.Workspace['Your map goes here']:GetChildren()) do

	local antiCheatClone = part:Clone()
	
	antiCheatClone.Size = Vector3.new(antiCheatCode.Size.X - 1.2, antiCheatCode.Size.Y - 1.2, antiCheatCode.Size.Z - 1.2)
	antiCheatClone.Touched:Connect(function(hit)

		if game.Players:GetPlayerFromCharacter(hit.Parent) then
			
			-- do something ig
		end
	end)
end

again this is just an idea of what you could do, some of it might be scuffed because i haven’t tested on this

A better way to check for noclip is raycasting down from the player and checking if the object is solid (CanCollide on)

I recommend only kicking the player if they are in the middle of the solid object or are making their way through one because sometimes players are able to stick their hands or legs through solid objects:

This is a great tutorial on explaining how to make good anti-cheats for you game I highly recommend checking it out: A Guide to Making Proper Anti-Exploits

2 Likes