• Script not working
    5 replies, posted
[lua] function BreakVehicleSpawning( pl, class, tbl ) if pl:IsAdmin() then end hook.Add( "PlayerSpawnVehicle", "BreakVehicles", BreakVehicleSpawning ) function FuckYouCrashCave() local shit = ents.FindInSphere( Vector( -8948, 13145, 250 ), 95201 ) if !shit then return end if v:IsPlayer() then v:SetPos( Vector( 4570, 13133, 129 ) ) v:ChatPrint( "Don't go near that crash cave." ) else v:Remove() end end end hook.Add( "Think", "FuckYouCrashCave", FuckYouCrashCave ) [/lua] anyone know why my script wouldnt be working?
First I'd advise indenting your code, it helps you see which end matches which condition, as it is your code is parsed thus: [lua]function BreakVehicleSpawning( pl, class, tbl ) if pl:IsAdmin() then end hook.Add( "PlayerSpawnVehicle", "BreakVehicles", BreakVehicleSpawning ) function FuckYouCrashCave() local shit = ents.FindInSphere( Vector( -8948, 13145, 250 ), 95201 ) if !shit then return end if v:IsPlayer() then v:SetPos( Vector( 4570, 13133, 129 ) ) v:ChatPrint( "Don't go near that crash cave." ) else v:Remove() end end end hook.Add( "Think", "FuckYouCrashCave", FuckYouCrashCave )[/lua] As you should be able to see you're hooking a function inside itself and then defining another function inside that function. I suspect you want something like this. [lua]function BreakVehicleSpawning( pl, class, tbl ) if pl:IsAdmin() then end end hook.Add( "PlayerSpawnVehicle", "BreakVehicles", BreakVehicleSpawning ) function FuckYouCrashCave() local shit = ents.FindInSphere( Vector( -8948, 13145, 250 ), 95201 ) if !shit then return end if v:IsPlayer() then v:SetPos( Vector( 4570, 13133, 129 ) ) v:ChatPrint( "Don't go near that crash cave." ) else v:Remove() end end hook.Add( "Think", "FuckYouCrashCave", FuckYouCrashCave )[/lua] In short, you put the end in the wrong place. In future I'd recommend either using local functions or anonymous functions for hooks. local functions so they can't conflict with another addon, anonymous functions so you're not storing the function when you don't need to. Here's your code using local for the first hook and anonymous for the second. [lua]-- this won't go in the global table now, if too many people put stuff in global it slows everything down local function BreakVehicleSpawning( pl, class, tbl ) if pl:IsAdmin() then end end hook.Add( "PlayerSpawnVehicle", "BreakVehicles", BreakVehicleSpawning ) hook.Add( "Think", "FuckYouCrashCave", function() local shit = ents.FindInSphere( Vector( -8948, 13145, 250 ), 95201 ) if !shit then return end if v:IsPlayer() then v:SetPos( Vector( 4570, 13133, 129 ) ) v:ChatPrint( "Don't go near that crash cave." ) else v:Remove() end end) [/lua]
Invisible for loops FTW?
[QUOTE=fishface60;19902621]anonymous functions so you're not storing the function when you don't need to.[/QUOTE] "Anonymous" functions get stored too. [img]http://imjustsayinblog.com/wp-content/uploads/2009/05/the_more_you_know2.jpg[/img]
[QUOTE=|FlapJack|;19904047]"Anonymous" functions get stored too.[/QUOTE] Correct, however what I meant was they are not stored where they don't need to be. In the case of using hooks the anonymous function is stored in the hook table. If you put it in the global table or as a local variable then the function will be stored there as well. If you don't need the function for anything other than the hook why save it. [editline]11:52PM[/editline] [QUOTE=decyg;19903739]Invisible for loops FTW?[/QUOTE] Ah, didn't notice that problem, I was indenting on automatic. Yes it does require a for loop to iterate over every ent in this sphere. [lua]-- this won't go in the global table now, if too many people put stuff in global it slows everything down local function BreakVehicleSpawning( pl, class, tbl ) if pl:IsAdmin() then end end hook.Add( "PlayerSpawnVehicle", "BreakVehicles", BreakVehicleSpawning ) hook.Add( "Think", "FuckYouCrashCave", function() local shit = ents.FindInSphere( Vector( -8948, 13145, 250 ), 95201 ) if !shit then return end for _, v in pairs(shit) do if v:IsPlayer() then v:SetPos( Vector( 4570, 13133, 129 ) ) v:ChatPrint( "Don't go near that crash cave." ) else v:Remove() end end end)[/lua] Apparently vehicles don't (or shouldn't) work on the server and there is a no-go area on the server this script is intended for.
Make sure you post errors next time. It helps us figure out what's wrong a lot faster, especially in more complicated code. :wink:
Sorry, you need to Log In to post a reply to this thread.