Take focus off of DFrame

I have a small popup ad in the corner of the screen before each round to help pay for my server. It works pretty well but for the first three seconds of the preround nobody can move because the cursor focus is on the DFrame. Is there anyway the focus can stay on the game, but players can still move around?


function Advert(ply)
	for k,v in pairs(player:GetAll()) do
		if ply:IsUserGroup("moderator", "user", "regular") then
			local window = vgui.Create( "DFrame" )
			window:SetPos(0, 0)
			window:SetSize(ScrW() * 0.2, ScrH() * 0.2)
			window:SetTitle("Advert")
			window:SetDraggable(false)
			window:ShowCloseButton(false)
			window:SetVisible( true )
			window:MakePopup()
			local html = vgui.Create("HTML", window)
			html:Dock(FILL)
			html:OpenURL("http://www.linkbucks.com/eLwr")
			timer.Simple( 3, function() window:Close() end)
		end
	end
end
hook.Add("TTTPrepareRound", "Advert", Advert(LocalPlayer()))

Pretty sure all you need to do is remove window:MakePopup().

I’m a little confused as to why you are looping through all the players when you are passing in a ply to advertise on anyways? You seem to be creating that box however many times there are players online.

I’m a lua novice. What would you recommend the code be changed to?

Just remove the loop altogether?



local function Advert(ply)
	if ply:IsUserGroup("moderator") or ply:IsUserGroup("user") or ply:IsUserGroup("regular") then
		local window = vgui.Create( "DFrame" )
		window:SetPos(0, 0)
		window:SetSize(ScrW() * 0.2, ScrH() * 0.2)
		window:SetTitle("Advert")
		window:SetDraggable(false)
		window:ShowCloseButton(false)
		window:SetVisible( true )
		local html = vgui.Create("HTML", window)
		html:Dock(FILL)
		html:OpenURL("http://www.linkbucks.com/eLwr")
		timer.Simple( 3, function() window:Close() end)
	end
end
hook.Add("TTTPrepareRound", "Advert", function()
    Advert(LocalPlayer())
end)


[editline]27th January 2016[/editline]

Also, another way to remove focus would just be

Panel:KillFocus, but that probably wouldn’t be very useful in this case since you created it as a popup

The last part of “hook.Add(“TTTPrepareRound”, “Advert”, Advert(LocalPlayer()))” wouldn’t work anyway, since you’re running the function when lua gets to that line, due to the “()”

Yeah, replace it with something like this:



hook.Add("TTTPrepareRound", "Advert", function()
    Advert(LocalPlayer())
end)


And it should work

[editline]27th January 2016[/editline]

Also, please make that Advert function local! It could be overwritten by another script or something accidentally if you don’t!

I tried this and it only works if the ‘if’ statement is commented out

I’m not familiar with the TTT codebase, so correct me if they’ve modified this function, but PLAYER:IsUserGroup() only takes a single argument, not varags.

I.e., ply:IsUserGroup(“moderator”, “user”, “regular”) doesn’t work because it only checks if the user is a moderator and disregards the rest of the arguments.

You’d need to check their user group individually for each group. Something like:


if ply:IsUserGroup("moderator") or ply:IsUserGroup("user") or ply:IsUserGroup("regular") then

A faster way to do this would be to create a table: (would also be very easy to add ranks this way)


local allowedRanks = {
[ "user" ] = true,
[ "regular ] = true,
[ "moderator" ] = true,
}

Then you would check if they were in the group:


if allowedRanks[ ply:GetUserGroup() ] then

Then you would put in the rest of your code.

u wanna fite m8??

[lua]if ({user=true,moderator=true})[ply:GetUserGroup()] then
– code
end[/lua]