Continuing after returning false

Hey I have this code:


local function DisableProps(ply)
	groups = sql.Query("SELECT * FROM prop_groups")
	
	for k,v in pairs(groups) do
		local groupss = sql.Query("SELECT * FROM prop_groups")[k]
		k,v = next(groupss)
		print(v)
		if ply:IsUserGroup(v) then
			ply:Kill() return true
		end
	end
	
end
hook.Add( "PlayerSpawnProp", "StopPlayersProp", DisableProps )


What happens with the current code is that when you attempt to spawn a prop it checks the prop_groups table for all results and then it does a for loop to go through all the different groups inside the column. If you are in the group and you spawned the prop it should kill you and spawn a prop which is what it does. But if you are not in a specific group and spawn a prop it just spawns the prop but doesn’t kill the player, which shows that the if function runs but it still returns true without breaking, but I’m not sure what do to. Could someone help me please? Thanks.

It isn’t returning true. Returning nothing will still be considered true.

Hook.add inserts your supplied function into a big list. When a prop is attempting to be spawned by a player, the game runs each function in the list one at a time. If a function returns anything, any not yet ran functions in the list for that hook event will not run.

So when you return nothing, the default value of true is applied at the end of the list.

[editline]14th September 2016[/editline]

Break it by returning false if your goal is to not spawn it. Do this by making the group check include an else.

Your title says you’re returning false, but the code shows otherwise.

Even if I remove the true statement so its


		if ply:IsUserGroup(v) then
			ply:Kill() return
		end


If I switch to a group that is not in the table, I can spawn a prop.

You need to return FALSE in order to stop the prop from spawning, PLEASE LOOK AT THE GODDAMN WIKI PAGE.

Returning nothing is not the same as returning true.

Not sure why you disagree with me. I said the same but also explained why.

[editline]14th September 2016[/editline]

You’re throwing a bit of a tantrum too. He’s just trying to learn, and didn’t understand the hook system.

Look if i had to be the main dev on gmod and deal with this shit then i’d get mad every now and then.

I explained that the end result is that the prop would still be allowed to spawn, similar to returning true. True (do spawn prop) is the default result of the hook. I explained why.

[editline]14th September 2016[/editline]

He doesn’t have to deal with a user on Facepunch asking a question.

He shouldn’t even bother if this is going to be his attitude about it.

Well next time explain it better because you might confuse someone that is new to Lua making him confuse himself with the returns


return

is the same as


return true

when its


return nil

So say that instead returning nothing/nil will literally do nothing.

I did say that. I explained how not returning results in the rest of the hook’s function list executing, and that this results in other code allowing the prop to spawn and that this has the same end result for his scenario as returning true, hence his confusion. While you guys bitch about semantics, I’m actually helping him to understand why his code didn’t work beyond telling him the solution.

This is the only post that “helped”. The others were rather confusing, or arguing.

Explaining how the hook system operates isn’t “confusing”, it’s necessary in order for OP to realize his mistake and for him to see how his assumptions were incorrect.

[editline]14th September 2016[/editline]

His assumption was that by hooking PlayerSpawnProp he would have total control over players spawning props in a way where no other code would matter. He was under the impression that by creating his hook, returning true was the only way to spawn a prop, and was confused why it was spawning in a scenario where he did not return true.

I explained how the hook system runs through many different functions fed in by hook.Add, and how returning a value interrupts the internal hook process to end on a given value.

Well next time explain yourself as you did now, saying that it will stop the hook internal process and specifying that returning nothing is the same as returning true IN THAT SPECIFIC HOOK, because you didn’t specify and that might get himself confused over other hooks. Not in all hooks returning nil/nothing will have the same effect as returning true.

Alright lets clarify some things, the code that I originally posted wasn’t the original code that I had intended to post. It looked a little like this


local function DisableProps(ply)
	groups = sql.Query("SELECT * FROM prop_groups")
	
	for k,v in pairs(groups) do
		local groupss = sql.Query("SELECT * FROM prop_groups")[k]
		k,v = next(groupss)
		print(v)
		if !ply:IsUserGroup(v) then
			ply:Kill() return false
		else
			ply:SetHealth(150) return true
		end
	end
	
end
hook.Add( "PlayerSpawnProp", "StopPlayersProp", DisableProps )

But I had a couple of changes to the code and didn’t change the title, my bad. But I still need help with this and would appreciate some friendly assistance. Okay so what the code is supposed to do is use the for loop running through the loop the amount of groups are in that table which at the current moment would be three. Then you have the second variable groupss that does what the first one does but has [k] (not sure why, someone could explain that would be nice) at the end, then the k,v pairs go through the table finding all the values. Then it prints v which is the group name and it runs the check to see if the player is in the group called v and if they are not they are killed and its returned false, if they are the health is set to 150 and they can spawn a prop. Now I have a table in the sv.db file called prop_groups now that contains a column called groups and that has three values that are in this order: admin, owner, moderator. No particular order just how I entered it in, now what happens is if I’m admin I can spawn a prop, but if I’m the other two I die and don’t spawn a prop. So what I have concluded is that the for loop is not running as I have planned it to and I’m trying to come up with a different way of doing this, if anybody has a solution or a suggestion please do respond. But do make sure it is actually helpful towards myself or any other person in the thread who needs help.

Nevermind your code for a minute.

What are you trying to accomplish with it?

What is the ultimate idea here, in short? Not described line by line, but in a sentence what you wanted to make.

Basically its a check to see if the player is a staff member so they can spawn props.

But… why kill them?

It’s just a test to see if it works type thing. If you want me to expand on this reply.

Either way this line in your code is unnecessary (You’re already iterating with pairs), and probably what’s fucking up everything [lua]k,v = next(groupss)[/lua]

Isn’t that line what makes the v change to the value that’s in the table? But for the sake of trying I will attempt it.

Edit:
As I expected it’s a table not a string so it doesn’t work.