I made my first ANTI cheat but when player dies it keeps printing 0,0,0 as velocity

local Players = game:GetService("Players")
local Player = Players.LocalPlayer
repeat wait() until Player.Character
local Character = Player.Character
local ReplicateStorage = game:GetService("ReplicatedStorage")
local IsHacking = ReplicateStorage.IsHacking
local Humanoid = Character:FindFirstChild("Humanoid")
local Hack

while Character do
	print(Character:FindFirstChild("HumanoidRootPart").Velocity)
	wait()
	if Humanoid.MaxHealth > 200 then
		Hack = "MaxHealth"
		print("Hacker")
		IsHacking:FireServer(Hack)
		wait(5)
		Player:Kick("Kicked for "..Hack)
	end
	if Humanoid.WalkSpeed > 26 or Character:WaitForChild("HumanoidRootPart").Velocity.X > 30 or Character:WaitForChild("HumanoidRootPart").Velocity.X < -30 or Character:WaitForChild("HumanoidRootPart").Velocity.Z < -30 or Character:WaitForChild("HumanoidRootPart").Velocity.Z > 30 then
		Hack = "WalkSpeed"
		print("Speed Hacker")
		IsHacking:FireServer(Hack)
		wait(5)
		Player:Kick("Kicked for "..Hack)
	end
	if Humanoid.JumpPower > 50 then
		Hack = "JumpPower"
		print("Jump Hacker")
		IsHacking:FireServer(Hack)
		wait(5)
		Player:Kick("Kicked for "..Hack)
	end
	if Character:WaitForChild("HumanoidRootPart").Velocity.Y > 80 or Character:WaitForChild("HumanoidRootPart").Velocity.Y < -80 then
		print("Hacker")
		Hack = "FlyHack"
		IsHacking:FireServer(Hack)
		wait(5)
		Player:Kick("Kicked for "..Hack)
	end

end
2 Likes

The character is changed every time the player resets or dies, for example:

Player character code: Character1
Player dies
Player character code: Character2


So everyime the player dies, reset the character variable:

Humanoid.Died:Connect(function()
    Character = player.Character
    if not Character then
        Character = player.CharacterAdded:Wait()
    end
end)

Note:
you shouldn’t be using:

repeat wait() until Player.Character

Instead you should use

Character = player.CharacterAdded:Wait()

Ok. but that is only called if player dies, how do i run it if player is alive?

You wouldn’t need to run it when the character is alive. Or if I didn’t answer correctly, can you elaborate?

So even if player did not die it would still run that function?

That function would automatically run when the player dies, no need to run the function manually.

Do i put the while in the function?

Use a player.CharacterAdded function instead so the script runs everytime the character respawns

1 Like

That fixed the problem Thanks guys!

1 Like