Adding a Timer

local google = vgui.Create( “DFrame” )
google:SetPos( 50, 50 )
google:SetSize( 1250, 850 )
google:SetTitle( “[WN] Advertisements” )
google:SetVisible( true )
google:SetDraggable( true )
google:ShowCloseButton( true )
google:MakePopup()

local html = vgui.Create(“HTML”, google)
html:SetPos( 10, 30)
html:SetSize(1225, 800)
html:OpenURL(“http://google.com”)

local DermaButton = vgui.Create( “DButton” )
DermaButton:SetParent( google )
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton:SetText(“Close”)
DermaButton.DoClick = function ()
Advertisements:Remove()
end

I have got this far but i can’t get the DermaButton on a timer. I need a timer that is set for like 30 sec, and after the 30 sec they can close the window and continue playing. I really need somebody to help me out with this coding. I’m new to all this so I’m trying for something easy

Thanks


timer.Simple( 30, function() --[[ draw stuff here ]] end )



button:SetEnabled( false );
local time = 30;
timer.Create( LocalPlayer():EntIndex().."button", time, 1, function()
    time = time - 1;
    button.SetText( "Close(" .. time ..")" );
    if( time == 0 ) then
        button:SetEnabled( true );
    end
end );


local Advertisements = vgui.Create( “DFrame” ) – Creates the frame itself
Advertisements:SetPos( 50, 50 ) – Position on the players screen
Advertisements:SetSize( 1250, 850 ) – Size of the frame
Advertisements:SetTitle( “[WN] Advertisements” ) – Title of the frame
Advertisements:SetVisible( true )
Advertisements:SetDraggable( true ) – Draggable by mouse?
Advertisements:ShowCloseButton( true ) – Show the close button?
Advertisements:MakePopup() – Show the frame

local html = vgui.Create(“HTML”, Advertisements)
html:SetPos( 10, 30)
html:SetSize(1225, 800)
html:OpenURL(“http://google.com”)

local DermaButton = vgui.Create( “DButton” )
DermaButton:SetParent( Advertisements ) – Set parent to our “Advertisements”
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton.DoClick = function ()
Advertisements:Remove()
end

DermaButton:SetEnabled( false );
local time = 1;
timer.Create( LocalPlayer():EntIndex()…“button”, time, 1, function()
time = time - 1;
DermaButton:SetText( “Close(” … time …")" );
if( time == 0 ) then
DermaButton:SetEnabled( true );
end
end );

This code for some reason doesn’t work. It pop up the screen and the button never becomes clickable or has the count down. It just says Label.

Can you please start using


 tags? You have an extra end at the end after creating the derma button and removing Advertisements.

First off, you need to make the close button invisible so people can’t just close out of the frame before the ad is finished. Second, you need to alter the duration of the timer to one second and the repetitions to 30. You also need to alter the time variable to the same number as the repetitions in the timer.

Edit:

code_gs, I don’t see the extra end. He has one to close the DoClick function, one to close the if statement, and one to close the timer function. My linter also reports no syntax errors.


local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
Advertisements:SetPos( 50, 50 ) -- Position on the players screen
Advertisements:SetSize( 1250, 850 ) -- Size of the frame
Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
Advertisements:SetVisible( true )
Advertisements:SetDraggable( true ) -- Draggable by mouse?
Advertisements:ShowCloseButton( false ) -- Show the close button?
Advertisements:MakePopup()  -- Show the frame 

local html = vgui.Create("HTML", Advertisements)
html:SetPos( 10, 30)    
html:SetSize(1225, 800)
html:OpenURL("http://motdgd.com/motd/?user=1924")                

local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( Advertisements ) -- Set parent to our "Advertisements"
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton:SetEnabled( false );
local time = 1;
timer.Create( LocalPlayer():EntIndex().."button", time, 30, function()
    time = time - 1;
    DermaButton:SetText( "Close(" .. time ..")" );
    if( time == 0 ) then
        DermaButton:SetEnabled( true );
    end
end );

DermaButton.DoClick = function () 
	Advertisements:Remove()
end

Can please explain what you mean. Because i don’t understand what you mean when you say change the time to match variables.

The time needs to be defined at the beginning to match the amount of time you want the button to be dysfunctional. Right now, time will be zero after the first rep. You need to make it be zero after the 30th rep. That’s why you need to replace time in the timer.create function with one.


local time = 30;

local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
Advertisements:SetPos( 50, 50 ) -- Position on the players screen
Advertisements:SetSize( 1250, 850 ) -- Size of the frame
Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
Advertisements:SetVisible( true )
Advertisements:SetDraggable( true ) -- Draggable by mouse?
Advertisements:ShowCloseButton( true ) -- Show the close button?
Advertisements:MakePopup()  -- Show the frame 

local html = vgui.Create("HTML", Advertisements)
html:SetPos( 10, 30)    
html:SetSize(1225, 800)
html:OpenURL("http://motdgd.com/motd/?user=1924")                

local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( Advertisements ) -- Set parent to our "Advertisements"
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton:SetText( "Close" )
DermaButton:SetDisabled( false );

timer.Create( LocalPlayer():EntIndex().."button", time, 30, function()
    time = time - 1;
    DermaButton:SetText( "Close(" .. time ..")" );
    if( time == 0 ) then
        DermaButton:SetDisabled( true )
    end
end );

DermaButton.DoClick = function () 
	Advertisements:Remove()
end

Ok can i get this explained easier because i am very new and have no idea what your talking about, I have got this far but it still won’t work. This show up with the close button allowed to be clicked with no timer or anything.



local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
Advertisements:SetPos( 50, 50 )
Advertisements:SetSize( 1250, 850 )
Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
Advertisements:SetVisible( true )
Advertisements:SetDraggable( true )
Advertisements:ShowCloseButton(false) -- Don't show the close button so the users can't just "X-out" of your frame
Advertisements:MakePopup()

local html = vgui.Create("HTML", Advertisements)
html:SetPos( 10, 30)    
html:SetSize(1225, 800)
html:OpenURL("http://motdgd.com/motd/?user=1924")                

local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( Advertisements )
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton:SetText( "Close" )
DermaButton:SetDisabled( true ); -- Should have this set to true to have the button disabled.
DermaButton.DoClick = function () 
	Advertisements:Remove()
end

time = 30 -- This needs to be set for the time you want the button to be unclickable.
-- timer.Create has 4 args: name, duration, reps, and the function that is called. If you want it to decrease by one every second, you need to have the reps set to 30 (30*1 second = 30 seconds) and the duration set to 1.
timer.Create( LocalPlayer():EntIndex().."button", 1, 30, function() 
    time = time - 1;    
if ( time == 0 ) then
        DermaButton:SetDisabled( false ) -- and this set to false to enable the button
    end
end );


Check the comments. I have tried to make it as simple as I can.


local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
Advertisements:SetPos( 50, 50 )
Advertisements:SetSize( 1250, 850 )
Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
Advertisements:SetVisible( true )
Advertisements:SetDraggable( true )
Advertisements:ShowCloseButton(true) -- Don't show the close button so the users can't just "X-out" of your frame
Advertisements:MakePopup()

local html = vgui.Create("HTML", Advertisements)
html:SetPos( 10, 30)    
html:SetSize(1225, 800)
html:OpenURL("http://motdgd.com/motd/?user=1924")                

local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( Advertisements )
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton:SetText( "Close" )
DermaButton:SetDisabled( true ); -- Should have this set to true to have the button disabled.
DermaButton.DoClick = function () 
	Advertisements:Remove()
end

time = 30 -- This needs to be set for the time you want the button to be unclickable.
-- timer.Create has 4 args: name, duration, reps, and the function that is called. If you want it to decrease by one every second, you need to have the reps set to 30 (30*1 second = 30 seconds) and the duration set to 1.
timer.Create( LocalPlayer():EntIndex().."button", 1, 30, function() 
    time = time - 1;    
if ( time == 0 ) then
        DermaButton:SetDisabled( false ) -- and this set to false to enable the button
    end
end ); 

I also wanted to add a timer things that show up on the button like
Close in 5
Close in 4
Close in 3
etc.

Like that on the button

This still don’t work the close the button is just faded

Sorry, that must have got snipped out in the copying. The



DermaButton:SetText( "Close(" .. time ..")" );


snippet should still work fine for changing that.


local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
Advertisements:SetPos( 50, 50 )
Advertisements:SetSize( 1250, 850 )
Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
Advertisements:SetVisible( true )
Advertisements:SetDraggable( true )
Advertisements:ShowCloseButton(true) -- Don't show the close button so the users can't just "X-out" of your frame
Advertisements:MakePopup()

local html = vgui.Create("HTML", Advertisements)
html:SetPos( 10, 30)     
html:SetSize(1225, 800)
html:OpenURL("http://motdgd.com/motd/?user=1924")                

local DermaButton = vgui.Create( "DButton" )
DermaButton:SetParent( Advertisements )
DermaButton:SetPos ( 525, 775 )
DermaButton:SetSize( 150, 50 )
DermaButton:SetDisabled( true ); -- Should have this set to true to have the button disabled.
DermaButton.DoClick = function () 
	Advertisements:Remove()
end

time = 30 -- This needs to be set for the time you want the button to be unclickable.
-- timer.Create has 4 args: name, duration, reps, and the function that is called. If you want it to decrease by one every second, you need to have the reps set to 30 (30*1 second = 30 seconds) and the duration set to 1.
timer.Create( LocalPlayer():EntIndex().."button", 1, 30, function() 
    time = time - 1;
	if ( time == 0 ) then
        DermaButton:SetDisabled( false )
		DermaButton:SetText( "Close" )
	else
		DermaButton:SetText( "Close in (" .. time ..")" );
		-- and this set to false to enable the button
    end
end );

Sorry but this won’t work for some reason and I can’t tell why.
It not setting the text, and the button just stays disabled.

Here’s your functioning, tested code. I’m kind of tired of trying to teach you at the moment.



local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
Advertisements:SetPos( 50, 50 )
Advertisements:SetSize( 1250, 850 )
Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
Advertisements:SetDraggable( true )
Advertisements:ShowCloseButton(false) -- Don't show the close button so the users can't just "X-out" of your frame
Advertisements:MakePopup()

local html = vgui.Create("HTML", Advertisements)
html:Dock(FILL)
html:OpenURL("http://motdgd.com/motd/?user=1924")                

local DermaButton = vgui.Create("DButton", Advertisements)
DermaButton:Dock(BOTTOM)
DermaButton:SetSize( 150, 50 )
DermaButton:SetDisabled( true ) -- Should have this set to true to have the button disabled.
DermaButton.DoClick = function() 
	Advertisements:Remove()
end
DermaButton:SetText("Close in (30)")


local time = 30 -- This needs to be set for the time you want the button to be unclickable. It should be local.
-- timer.Create has 4 args: name, duration, reps, and the function that is called. If you want it to decrease by one every second, you need to have the reps set to 30 (30*1 second = 30 seconds) and the duration set to 1.
timer.Create( LocalPlayer():EntIndex().."button", 1, 30, function() 
	time = time - 1
	 if ( time == 0 ) then
		DermaButton:SetDisabled( false )
		DermaButton:SetText( "Close" )
	else
		DermaButton:SetText( "Close in (" .. time ..")" )-- and this set to false to enable the button
	end
end)


Sorry for being so annoying but this is still not working for me. http://prntscr.com/79g1f4 that a picture of what i get, the count-down does not work, and you can’t press the button after 30 second or even a minute. When it set to 0 the button does not work then either. Also get this error in console. http://prntscr.com/79g2a0

I noticed that you kept editing the code to add more to the problem after I already told you what needed to be changed. I just tested out that exact code and it did exactly what you asked it to do. Have you made any changes to the code?

I put the code that you sent. I have not changed it at all.

I just tested it out again and it works fine. It must be something on your end that is causing problems. Are you getting any Lua errors in console? Here’s a picture of what I get: http://i.imgur.com/pgcJbGq.jpg

I get this error. http://prntscr.com/79g2a0. How are you running the script? Because that what i think that may be causing it

This is just a in a console command so I can easily test it out. Here is the full code:



concommand.Add('OpenAdvertisements', function()
	local Advertisements = vgui.Create( "DFrame" ) -- Creates the frame itself
	Advertisements:SetPos( 50, 50 )
	Advertisements:SetSize( 1250, 850 )
	Advertisements:SetTitle( "[WN] Advertisements" ) -- Title of the frame
	Advertisements:SetDraggable( true )
	Advertisements:ShowCloseButton(false) -- Don't show the close button so the users can't just "X-out" of your frame
	Advertisements:MakePopup()

	local html = vgui.Create("HTML", Advertisements)
	html:Dock(FILL)
	html:OpenURL("http://motdgd.com/motd/?user=1924")                

	local DermaButton = vgui.Create("DButton", Advertisements)
	DermaButton:Dock(BOTTOM)
	DermaButton:SetSize( 150, 50 )
	DermaButton:SetDisabled( true ) -- Should have this set to true to have the button disabled.
	DermaButton.DoClick = function() 
		Advertisements:Remove()
	end
	DermaButton:SetText('Close in (30)')


	local time = 30 -- This needs to be set for the time you want the button to be unclickable. It should be local.
	-- timer.Create has 4 args: name, duration, reps, and the function that is called. If you want it to decrease by one every second, you need to have the reps set to 30 (30*1 second = 30 seconds) and the duration set to 1.
	timer.Create( LocalPlayer():EntIndex().."button", 1, 30, function() 
		time = time - 1
		 if ( time == 0 ) then
			DermaButton:SetDisabled( false )
			DermaButton:SetText( "Close" )
		else
			DermaButton:SetText( "Close in (" .. time ..")" )-- and this set to false to enable the button
		end
	end)
end)


How are you executing the code that makes it?