Error from making a function serverside and using it on client side

I’m sorry if this sounds dumb, but I’m having a problem creating a function on server side and trying to use it on client side, and I’m getting an error from it.



if SERVER then
    util.AddNetworkString("YoutubeMSG")

    hook.Add("PlayerSay", "PlayerSayExample", function(ply, text, team)
        if (string.sub(text, 1, 8) == "/playnow") then
            local DankMemes = string.sub(text, 9)
        
            net.Start("YoutubeMSG")
                dankMemes = string.sub(text, 9)
            net.Send(ply)
            
            return "Now Playing: " .. string.sub(text, 9)
        end
    end)
else
    net.Receive("YoutubeMSG", function()
            local frame = vgui.Create("DFrame")
            frame:SetTitle("youtube")
            frame:SetSize(ScrW() * 0.75, ScrH() * 0.75)
            frame:Center()
            frame:MakePopup()


            local html = vgui.Create("HTML", frame)
            html:Dock(FILL)
            html:OpenURL(dankMemes())
    end)
end


The error is:



[ERROR] addons/player/lua/autorun/youtubeplayer.lua:26: attempt to call global 'dankMemes' (a nil value)
  1. func - addons/player/lua/autorun/youtubeplayer.lua:26
   2. unknown - lua/includes/extensions/net.lua:32


You’re not using the net library correctly. Use

net.WriteString and

net.ReadString.



if SERVER then

	util.AddNetworkString( "YoutubeMSG" )

	hook.Add( "PlayerSay", "PlayerSayExample", function( ply, text, team )
		if string.sub( text, 1, 8 ) == "/playnow" then
			local DankMemes = string.sub( text, 10 )
			net.Start( "YoutubeMSG" )
			net.WriteString( DankMemes )
			net.Send( ply )
			return "Now Playing: " .. DankMemes
		end
	end )

else

	net.Receive( "YoutubeMSG", function()

		local frame = vgui.Create( "DFrame" )
		frame:SetTitle( "YouTube" )
		frame:SetSize( ScrW() * 0.75, ScrH() * 0.75 )
		frame:Center()
		frame:MakePopup()

		local html = vgui.Create( "HTML", frame )
		html:Dock( FILL )
		html:OpenURL( net.ReadString() )

	end )

end


[editline]12th November 2017[/editline]

Also, you don’t need to use the net library at all. Use

GM:OnPlayerChat.



hook.Add( "OnPlayerChat", "YouTubeMSG", function( ply, text, team, dead )

	if ply ~= LocalPlayer() then return end

	if string.sub( text, 1, 8 ) == "/playnow" then
		
		local frame = vgui.Create( "DFrame" )
		frame:SetTitle( "YouTube" )
		frame:SetSize( ScrW() * 0.75, ScrH() * 0.75 )
		frame:Center()
		frame:MakePopup()

		local html = vgui.Create( "HTML", frame )
		html:Dock( FILL )
		html:OpenURL( string.sub( text, 10 ) )

		return true

	end

end )


The openURL isn’t able to open the url because it’s not encased in “” I’m pretty sure.

You don’t need to encase a string that’s already a string in quotes, E.G. this doesn’t work for obvious reasons:



local str = "this is already in quotes"
print("str")


Using that code, it says [General] The command ‘playnow’ is not a valid command.
in chat

Sorry, found the problem.



local DankMemes = string.sub( text, 9 )


Should be



local DankMemes = string.sub( text, 10 )


Because there’s a space between the command and the actual website link.

I updated my posts so they should both work now.

[EDITLINE]12 November[/EDITLINE]

By the way, by default, Garry’s Mod never tells you whether or not your chat commands are valid.
This is because by default Garry’s Mod has no concept or library defining what a chat command is.
It seems you have some sort of command addon that may be interfering with this code.
If this addon has a different way of adding a command, you should probably use it to avoid problems.

Snip - dumb comment miss interpreted what u said xD

Yeah, I’m using the Skill Surf gamemode from flow and it doesn’t let me use client side commands, but thank you the serverside fix worked.

:snip: automerge again lol sorry