Hooks

I am trying to learn Lua, but in the tutorials on the garrysmod wiki do not explain what hooks are.

Can someone explain what hooks are, and/or add a tutorial there explaining them?

Hooks “hook” into certain functions…

Here’s an example, dono if it will make sense.

Function - This is a normal function, no hooks.
[lua]
function GM:PlayerSpawn§
Msg("hi!
")
end
– When you spawn you will see the message - hi! - in your console.
[/lua]

Hook
[lua]
function MySpawnFunction§
Msg(“How are you?”)
end
hook.Add(“PlayerSpawn”, “HookName”, MySpawnFunction)
– I just “hooked” into the PlayerSpawn function, no when you spawn
– you will see.
– hi!
– How are you?
[/lua]

Hope that makes sense.

Basically this hook system lets you set up a function to be run when a certain event happens, like a player dying, an entity taking damage, a prop being spawned etc. So if you wanted to ban a model you’d add a prop spawn hook that checks the model and blocks the prop from being spawned if it’s a banned model.

If this clarifies: a hook is a function that is called when the function you have hooked into is called.

Which is exactly why I wonder why most aimbots actually use hooks. Mine doesn’t :wink: