Gmod crash... Basic clientside script... Help is appreciated!

So i have this basic lua hack. The problem with it, is that every time i press MOUSE_5 like i set the aimbot trigger to be, it crashes my game and obv. leaves no error message. Please help. Full lua code under |


local hCreateClientConVar = CreateClientConVar


local function CreateClientConVar(cvarname,default,save,onmenu)
	hCreateClientConVar(cvarname, default, save, false)
	return {cvar = GetConVar(cvarname), OnMenu = onmenu, name = cvarname, main = string.find(cvarname, "enable")}
end


local prefix = "LKZ"
local chatPrefix = "[LKZ] : "
local players = player.GetAll()
local lPly = LocalPlayer()
local TraceRes = nil
local headPos = nil
local target = nil
local mouse1 = false
local AimbotKeyDown = false
local color = Color(255, 100, 50)
local mat = CreateMaterial(string.lower("sdfkowasdlfl"), "VertexLitGeneric", {["$basetexture"] = "models/debug/debugwhite", ["$model"] = 1, ["$ignorez"] = 1})
local text = chat.AddText


CreateClientConVar(prefix.."_chams", "1", true, false)
CreateClientConVar(prefix.."_aimAtFriend", "0", true, false)
CreateClientConVar(prefix.."_maxAngle", "30", true, false)
CreateClientConVar(prefix.."_aimbot", "1", true, false)
CreateClientConVar(prefix.."_aimOnKey", "0", true, false)
CreateClientConVar(prefix.."_aimOnMouse1", "0", true, false)


function GetValidPlayers()
	local player = {}


	for _, ply in pairs(players) do
		if (ply != lPly && IsValid(ply) &&
		ply:IsPlayer() && ply:Alive() && ply:Health() >= 1 &&
		(ply:GetFriendStatus() != "friend" || GetConVar(prefix.."_aimAtFriend"):GetInt() == 1)) then
			table.insert(players, ply)
		end
	end
	return players
end


function IsVisible(ply)
	if (!IsValid(ply)) then return false end
	
	local vecPos, _ = ply:GetBonePosition(ply:LookupBone("ValveBiped.Bip01_Head1") or 12)
	local trace = {start = lPly:EyePos(), endpos = vecPos, filter = lPly, mask = MASK_SHOT}
	local traceRes = util.TraceLine(trace)


	TraceRes = traceRes


	if (traceRes.HitWorld || traceRes.Entity != ply) then return false end


	return true
end


function VelocityPrediction(ply) return ply:GetAbsVelocity()*0.012 end


function ClosestAngle(players)
	local flAngleDifference = nil
	local newAngle = nil
	local viewAngles = lPly:EyeAngles()


	for _, ply in pairs(players) do
		local vecPos, ang = ply:GetBonePosition(ply:LookupBone("ValveBiped.Bip01_Head1") or 12)
		local oldpos = vecPos
		vecPos = vecPos-VelocityPrediction(lPly)+VelocityPrediction(ply)
		local angAngle = (vecPop-lPly:EyePos()):Angle()
		local flDif = math.abs(math.AngleDifference(angAngle.p, viewAngles.p))+math.abs(math.AngleDifference(angAngle.y, viewAngles.y))


		if ((flAngleDifference == nil || flDif < flAngleDifference) && (GetConVar(prefix.."_maxAngle"):GetInt() == 0 || flDif < GetConVar(prefix.."_maxAngle"):GetFloat())) then
			headPos = oldpos:ToScreen()
			target = ply
			flAngleDifference = flDif
			newAngle = angAngle
		end
	end
	return newAngle
end


function Aimbot()
	headPos = nil


	if (GetConVar(prefix.."_aimbot"):GetInt() == 0 || !mouse1 && (GetConVar(prefix.."_aimOnKey"):GetInt() == 1 || GetConVar(prefix.."_aimOnMouse1"):GetInt() == 1) && !AimbotKeyDown) then return end


	local players = {}


	for _, ply in pairs(GetValidPlayers()) do
		if (IsVisible(ply)) then
			table.insert(players, ply)
		end
	end


	if (table.Count(players) == 0) then
		target = nil
		return
	end


	local newAngle = ClosestAngle(players)


	if (newAngle != nil) then lPly:SetEyeAngles(newAngle) end
end


function CreateMove(cmd)
	if (cmd:KeyDown(IN_ATTACK) && !mouse1 && GetConVar(prefix.."_aimOnMouse1"):GetInt() == 1) then
		mouse1 = true
		Aimbot()
	elseif (!cmd:KeyDown(IN_ATTACK) && mouse1 && GetConVar(prefix.."_aimOnMouse1"):GetInt() == 1) then
		mouse1 = false
	end
end


function TableSortByDistance(former,latter) return latter:GetPos():Distance(lPly:GetPos()) > former:GetPos():Distance(lPly:GetPos()) end


function GetPlayersByDistance()
	local plyers = players


	table.sort(players, TableSortByDistance)


	return players
end


function AddToColor(color,add) return color+add <= 255 and color+add or color+add-255 end


function Chams()
	if (GetConVar(prefix.."_chams"):GetInt() == 1) then
		for _, ply in pairs(GetPlayersByDistance()) do
			if (IsValid(ply) && ply:Alive() && ply:Health() > 0 && ply:Team() != TEAM_SPECTATOR) then
				local color = Color(200, 50, 50) or team.GetColor(ply:Team())


				cam.Start3D(lPly:EyePos(), lPly:EyeAngles())
					render.SuppressEngineLighting(true)


					render.SetColorModulation(color.r/255, color.g/255, color.b/255, 1)
					render.MaterialOverride(mat)
					ply:DrawModel()


					render.SetColorModulation(AddToColor(color.r, 150)/22, AddToColor(color.g, 150)/255, AddToColor(color.b, 150)/255, 1)
					if (IsValid(ply:GetActiveWeapon())) then
						ply:GetActiveWeapon():DrawModel()
					end


					render.SetColorModulation(1, 1, 1, 1)
					render.MaterialOverride()
					render.SetModelLighting(4, color.r/255, color.g/255, color.b/255)
					ply:DrawModel()


					render.SuppressEngineLighting(false)
				cam.End3D()
			end
		end
	end
end


hook.Add("RenderScreenspaceEffects", "Chams", Chams)
hook.Add("Think", "Aimbot", Aimbot)
hook.Add("CreateMove", "CreateMove", CreateMove)
hook.Add("Think", "AimbotKeyDown", function()
	if input.IsMouseDown(MOUSE_5) == true then
		AimbotKeyDown = true


		Aimbot()
	else
		AimbotKeyDown = false
	end
end)


concommand.Add(prefix.."_unload", function()
	hook.Remove("RenderScreenspaceEffects", "Chams")
	hook.Remove("Think", "Aimbot")
	hook.Remove("CreateMove", "CreateMove")


	text(color, chatPrefix.."All hooks unloaded successfully!")


	concommand.Remove(prefix.."_chams")
	concommand.Remove(prefix.."_aimbot")
	concommand.Remove(prefix.."_maxAngle")
	concommand.Remove(prefix.."_aimOnMouse1")
	concommand.Remove(prefix.."_aimOnKey")
	concommand.Remove(prefix.."_aimAtFriend")
	concommand.Remove(prefix.."_unload")


	text(color, chatPrefix.."All convars unloaded successfully!")
	text(color, chatPrefix.."
Cya later m8!")
end)


do you really want us to read all your hack without you even understanding how does it works, and expect us to fix it for you? What?

Dont cheat lmao

The whole script is a mess. You’re better off completely rewriting it than trying to fix all of the issues.

What website you take this code from, just to realise it didnt work


function VelocityPrediction(ply) return ply:GetAbsVelocity()*0.012 end

Wouldn’t it be better to take difference between current and previous frames and add that difference to the current one, not just 0.012?

engine.TickInterval, but still unreliable prediction, you’re better off either using an engine prediction module or rewriting the whole sourcesdk prediction stuff in lua, and you only predict LocalPlayer btw