Lua Questions

Hey Guys,
Ok, so i am pretty new to gmod, but i am not to scripting.

Anyways, i was thinking let my try something out.
So i want to make a basic Clientside only script.

I was on a CSS server once and they had something called “yturl” also known as youtube url.
Basically what it does is if you type:

!yturl -_WHAbikzf4

then it would open it in a small window and it would start playing the youtube url

http://www.youtube.com/watch?v=-_WHAbikzf4 this one then.

I have looked around the wiki and i found this, which i still need to test.

So now i want to know. Is…

What kind of command do i use to check if i typed: “!yturl -_WHAbikzf4”
I was thinking maybe of checking if the console wrote it and then open that and.

Of course i want to be able to close the window without the music stopping.
So the window needs to be “hidden” or 100% see through or something.

Anyways, i am hoping some people can launch me into the right direction.
I hope i gave enough information so you guys can help me out.

Best Regards
Blaxus

You will need a OnPlayerChat hook, and the HTML derma controls.

Unfortunately, HTML does not refresh the page without user input (So you need to use the laggier and hackier method of changing the size on think; unless you are going to think about using AzuiSleet’s Chrome module.

Ok so i got it work, but to do it again i need to close the window when the song has ended.
Here is my code…


--[[
		============================
Product: Youtube Player
Author : Blaxus
Version: 0.1
		============================
]]

// Sart function Play Youtube Url
function playturl(player, text, team, dead)
	if player == LocalPlayer() then
		
		// if player said !yturl activate controle
		if string.sub(text,0,6) == "!yturl" then
			
			// Variable Containing the link to the youtube url
      tur = string.sub(text,8,19)
      
      // Make the panel
      HTMLTest = vgui.Create("HTML")
      // Open the desired link
      HTMLTest:OpenURL("http://www.youtube.com/watch?v=" .. tur .. "")

      // Print in console what link we opened
      print("Youtube Gmod Player Is now opening: http://www.youtube.com/watch?v=" .. tur .. "")

		end
		
		// Check if user wants to stop playing
    if string.sub(text,0,3) == "!sp" then
        
      tur = string.sub(text,0,3)

      HTMLTest:clear("HTML")

    end	
	

	// close if localplayer
	end
	
// Close function
end

// Hook the actions
hook.Add("OnPlayerChat" , "playturl" , playturl)

I keep getting

Hook ‘playturl’ Failed: autorun/client/yturl.lua:34: attempt to call method ‘clear’ (a nil value)

i used “html” this time, but before it gave the same error and it was just:

HTMLTest:clear()

HTMLTest:Clear()

Lua is case sensitive.

Although, I’m not sure this will achieve the end result you desire; it only says it clears text. I’ve never tried this method though (I normally open a blank URL to do this) If it does work, this could be very interesting.

Your right, it was Case sensetive, however, the console returns to me:

HTML:Clear doesn’t work right now (Use SetHTML)

But when i do any of these, my entire script does not work anymore.

HTMLTest::SetHTML()
HTMLTest::SetHTML("")
HTMLTest::SetHTML( “<p>Youtube Player Stopped!</p>” )

EDIT: Sorry my bad, i had double times this “::” which caused the error.

It is fully working now,
Anyone who wants to use this, feel free to do so.

Usage: type these commands ingame

Play Youtube Song: !yturl link of youtube code
Stop Playing: !sp

Example: !yturl SpkBVFo-fWE will start playing this link:
http://www.youtube.com/watch?v=SpkBVFo-fWE

everything behind: watch?v= is what you need to copy, and then it will start playing.
I have no control over the volume though.

Complete Script:


--[[
		============================
Product: Youtube Player
Author : Blaxus
Version: 0.1
		============================
]]

// Sart function Play Youtube Url
function playturl(player, text, team, dead)
	if player == LocalPlayer() then
		
		// if player said !yturl activate controle
		if string.sub(text,0,6) == "!yturl" then
			
			// Variable Containing the link to the youtube url
      tur = string.sub(text,8,19)
      
      // Make the panel
      HTMLTest = vgui.Create("HTML", DPanel)
      // Open the desired link
      HTMLTest:OpenURL("http://www.youtube.com/watch?v=" .. tur .. "")

      // Print in console what link we opened
      print("Youtube Gmod Player Is now opening: http://www.youtube.com/watch?v=" .. tur .. "")

		end
		
		// Check if user wants to stop playing
    if string.sub(text,0,3) == "!sp" then

      // Close Window
      HTMLTest:SetHTML( "<p>Youtube Player Stopped!</p>" )

    end	
	

	// close if localplayer
	end
	
// Close function
end

// Hook the actions
hook.Add("OnPlayerChat" , "playturl" , playturl)

Save as “yturl.lua” in “C:\Program Files\Steam\steamapps*username*\garrysmod\garrysmod\lua\autorun\client”
Have fun listening. :slight_smile:

And thanks FlapJack :wink:

To stop it, try

HTMLTest:OpenURL(“http://www.google.com”)

That is what I uses for my streaming radio.

Yes, but then you would have to load google first, ok it is not a slow site.
But that would be unnecessary because you asked it to stop to the player. Not to visit google.

If google at the time of your visit has a problem of some sorts it could cause lagg as your player is trying to reach that website. If i am correct.

Try HTMLTest:OpenURL( “about:blank” )

Console returns to me, “not opening: about:blank”
But like i said before.

I got it to work with SetHTML :slight_smile: